What should compressionQuality be when using UIImageJPEGRepresentation?

偶尔善良 提交于 2019-12-20 11:24:05

问题


I would like to apply a filter to a photo from the user's library then write it back to disk. I'm using UIImageJPEGRepresentation. This function takes a UIImage and a compressionQuality value between 0.0 and 1.0. Because I want to preserve the original quality, I set it to 1.0. But I found this actually creates an image file that's larger in file size than the original, somewhat significantly larger even. I noticed in Apple's Sample Photo Editing Extension app they always set the quality to 0.9. I tried that and it does output a file size close to the original photo file size. This doesn't make sense to me. Can someone explain this? Is it appropriate to always use 0.9? Does the iOS camera compress it using that value and thus 0.9 is the original quality? Or why isn't 0.9 actually 1.0, if there's nothing that's better quality?

NSData *renderedJPEGData = UIImageJPEGRepresentation(image, 0.9f);

回答1:


This is an old question and you've probably worked this out for yourself, but if anyone's still interested, this might help.

(I'm not a JPEG expert)

UIImageJPEGRepresentation isn't taking into consideration how the image may have been compressed in the past, it's just compressing the image that it has at the time.

You mention a photo that's "from the user's library". The user could have any image in their library, compressed at any compressionQuality.

To try to keep the image at the same file size, you'd need to know how it was originally saved, and even if you use the same compressionQuality, you will likely result in a different file size because the data that's being compressed is different.

As for using a compressionQuality of 0.9 for saving images, it depends on how much you value the quality of your image versus how much you space you want to use. 0.7 seems to be often suggested as a good balance.

I'm not sure how much the iOS camera app compresses the image (if at all) and I'm not sure that it is guaranteed to be the best possible quality. If it is using 0.9, you could always create an app that takes a photo and compresses it less before saving.

I hope that helps.




回答2:


You can convert image in PNG representation for original image quality.

UIImagePNGRepresentation(image)


来源:https://stackoverflow.com/questions/26330492/what-should-compressionquality-be-when-using-uiimagejpegrepresentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!