将 UIImage 大小减少到可管理的大小(减少字节) [英] Reduce UIImage size to a manageable size (reduce bytes)

查看:41
本文介绍了将 UIImage 大小减少到可管理的大小(减少字节)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想减少设备捕获的图像的字节数,因为我相信 _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);

生成编码为 PNG 的图像的 NSData 版本(很容易插入到 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的图像质量.这两者都应该生成比 UIImage 更小的内存图像.

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,我强烈建议您不要使用它.有关在记录的方法中起作用的方法,请参阅这篇文章.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆