Question

I am working on a portion of an app that has uses an ImagePickerViewController to take pictures/use existing pictures for a profile page. I am reducing the size of the resolution of the pictures (all JPEGs) to 480x320 and than compressing it fine. The problem is that I have no way to determine which photos need to have resolutions reduced and compressed.

Now what I want to do is write an algorithm to reduce the resolution of a picture if it is larger than 480x320 and adjust the compression level of a picture so that it will be around 30-50 KB in size. The compression level will scale with the size of the picture so anything less than 30-50 KB has no compression, anything more has as much compression as needed to hit 30-50 KB.

How would I go about writing an algorithm to handle this?

Was it helpful?

Solution

You'll need to do trial and error -- resize the image to 480x320, then save it as a JPEG at, say, 95% quality. Check the file size and if it's too big, try again at 90%. Repeat until you get into the right range.

This will potentially be relatively slow (several seconds?) so do this on a background thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top