2013-12-05

I am looking to downscale a UIImage in iOS.

I have seen other questions below and their approach on how to downscale the image by size.
Resizing Images Objective-C
How to resize the image programatically in objective-c in iphone
The simplest way to resize an UIImage?

These questions are all based on re-sizing the image to a specific size. In my case I am looking to re-size/downscale the image based on a maximum size.

As an example, I would like to set a maximum NSData size to be 500 KB. I know that I can get the size of the image like this:// Check the size of the image returned

What I would like to do is some form of loop here. If the size is greater than the maximum size that I set, I would like to scale down the image slightly, then check the size. If the size is still too large scale down again slightly then check again, until it is lower than the maximum set size.

I am not sure what the best approach for this is. Ideally I do not want to scale down the image to a specific size all the time, but only slightly scale down the image each time. That way I can have the largest (size w/h) of the image itself and at its maximum size (bytes). If I scale down slightly only at a time, what would be the best way to accomplish this?

EDIT
To confirm, I am looking to re-size the actual image but re-size the image so that it is smaller than the maximum NSData Length.
For example:

-Check the NSData Length

-If above the maximum I want to pass the UIImage into a method

-Then loop through this method slightly re-sizing the actual image size each time

-Until it is under the maximum NSData length, then return the image?

Show more