CIImage compression to JPEG

I am trying to compress a CIImage below a certain file size threshold when creating a JPEG. Currently, I am handling this in, potentially, multiple passes. In the first pass, I am essentially calling:
encodeJPEG(CIImage, quality: 0.85, colorSpace: CIImage.colorSpace ?? CGColorSpace(name: CGColorSpace.sRGB)!)
I am then checking the image size. If the image is still too big, I try to estimate how much I need to reduce it and try again until the image size is small enough.

My question: Is there a more efficient way of doing this?

Answered by Engineer in 893430022

This iterative approach is the best way to go. Another approach is to do something like the save panel in Preview.app which shows a live estimate of the compressed file size as the quality is adjusted.

Accepted Answer

This iterative approach is the best way to go. Another approach is to do something like the save panel in Preview.app which shows a live estimate of the compressed file size as the quality is adjusted.

Currently there is not a more efficient way to compress to a desired size. You're approach is a fine way of doing this currently.

One could file an enhancement request using Feedback Assistant. Once you file the request, please post the FB number here.

If you're not familiar with how to file enhancement requests, take a look at Bug Reporting: How and Why?

Also you may be able to use downscaled proxy size image and encode at a reference quality and the required bytes-per-pixel for the proxy size can be used as a predictor of the full-res size at the same quality given compressibility is driven by content, not resolution. Potentially to speed up the process

CIImage compression to JPEG
 
 
Q