Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should compressionQuality be when using UIImageJPEGRepresentation?

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);
like image 569
Jordan H Avatar asked Oct 12 '14 22:10

Jordan H


1 Answers

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.

like image 79
Josh Paradroid Avatar answered Nov 10 '22 07:11

Josh Paradroid