简体   繁体   English

在iOS中以编程方式压缩PNG图像

[英]Compressing PNG image programmatically in iOS

I need to send to server png images with 300DPI and exactly 1275x1875pixels in size. 我需要将300DPI和大小恰好为1275x1875pixels的png图像发送到服务器。 Because by default UIKit will create image with 72DPI, I need to set scale factor to 300/72 in UIKit function UIGraphicsBeginImageContextWithOptions to be able to get 300DPI image, and after this I'm using UIImagePNGRepresentation function to get NSData of image and write it in file. 因为默认情况下,UIKit将使用72DPI创建图像,所以我需要在UIKit函数UIGraphicsBeginImageContextWithOptions中将比例因子设置为300/72,才能获取300DPI图像,此后,我将使用UIImagePNGRepresentation函数获取图像的NSData并将其写入文件。 For now I can get desired image size and DPI (have checked in photoshop), but it becomes almost 5mb. 现在,我可以获得所需的图像大小和DPI(已在photoshop中检查),但它几乎达到了5mb。 Is there any way to reduce PNG image size in iOS? 有什么办法可以减少iOS中的PNG图片大小?

Thanks in advance! 提前致谢!

If you need compression, why not send those images as JPEG instead? 如果需要压缩,为什么不以JPEG格式发送这些图像呢?

NSData * UIImageJPEGRepresentation ( UIImage *image, CGFloat compressionQuality );

Set compressionQuality to 0f for highest compression, 1f for lowest. 将compressionQuality设置为0f表示最高压缩,将1f表示最低。 Convert back to PNG when you need a PNG. 需要PNG时转换回PNG。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM