iOS 从 nsdata 压缩图像
我有一个下载 jpeg 图像的应用程序,但它们很大,大于应用程序所需的大小。我创建了一个类来下载图像的 NSData。获取 NSData 并压缩图像的最佳方法是什么?
到目前为止,我见过的唯一途径是将图像临时写入磁盘,访问它并压缩磁盘上的 jpeg。
有没有一种替代方法,您不必写入磁盘并直接压缩它并接收数据?
I have an application that downloads jpeg images, but they're large, larger than necessary for the application. I've created a class that'll download the NSData of the image. What's the best way to take the NSData and compress the image.
So far the only route i've seen is to write the image to disk temporarily, access it and compress the jpeg on disk.
Is there an alternative way where you don't have to write to disk and compress it directly and the data is received?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好在应用程序下载图像之前对其进行压缩。但是,如果这超出了您的控制范围,请稍后压缩它们。
要将图像转换为另一种格式,您可以使用 CGImageDestination.h 中定义的函数
,我将创建一个转换给定图像的类方法函数:
图像类型可以是 kUTTypePNG,但质量参数很可能不起作用。根据您的需要,它也可以是 kUTTypeJPEG2000。
您可以使用数据初始化新图像或将其保存到磁盘。
It would be better to compress the image before your application downloads it. However, if this is beyond your control, compress them after.
To convert an image to another format you can use functions defined in CGImageDestination.h
I would make a function of class method that converts a given image:
The image type can be kUTTypePNG, but the quality parameter most likely will not work. It also can be kUTTypeJPEG2000 for your needs.
You can initialise a new image with the data or save it to disk.
如果您问我认为您在问什么,您可以使用
+imageWithData:
使用 NSData 块创建 UIImage 对象。If you're asking what I think you're asking, you can create UIImage objects with a chunk of NSData using
+imageWithData:
.