将UIImage大小减小到可管理的大小(减少字节) [英] Reduce UIImage size to a manageable size (reduce bytes)
问题描述
我想减少由设备捕获的图像的字节数,因为我相信 _imageScaledToSize
不会减少图片的字节数(或它?) - 我想在本地词典对象中存储图像的缩略图,并且不能将全尺寸图像放在字典中。任何想法?
I want to reduce the number of bytes of an image captured by the device, since i believe the _imageScaledToSize
does not reduce the number of bytes of the picture (or does it?) - i want to store a thumbnail of the image in a local dictionary object and can't afford to put full size images in the dictionary. Any idea?
推荐答案
如果您只想压缩UIImage,可以使用
If you wish to simply compress your UIImage, you can use
NSData *dataForPNGFile = UIImagePNGRepresentation(yourImage);
生成您的图像的NSData版本,编码为PNG(容易插入NSDictionary或写入磁盘),或者您可以使用
to generate an NSData version of your image encoded as a PNG (easily inserted into an NSDictionary or written to disk), or you can use
NSData *dataForPNGFile = UIImageJPEGRepresentation(yourImage, 0.9f);
做同样的,只有JPEG格式。第二个参数是JPEG的图像质量。
to do the same, only in a JPEG format. The second parameter is the image quality of the JPEG. Both of these should produce images that are smaller, memory-wise, than your UIImage.
使用已发布的方法调整UIImage的大小以创建较小的缩略图(以像素为单位)是一个更小的缩略图有点棘手。 _imageScaledToSize来自私人API,我强烈建议你不要使用它。有关在已记录的方法中工作的方法,请参阅此帖< a>。
Resizing a UIImage to create a smaller thumbnail (pixels-wise) using published methods is a little trickier. _imageScaledToSize is from the private API, and I'd highly recommend you not use it. For a means that works within the documented methods, see this post.
这篇关于将UIImage大小减小到可管理的大小(减少字节)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!