At the very least, it makes no sense to select and release the image each time you go through the loop. This should not be a memory leak, but it is optional, so move alloc / init and exit the loop.
In addition, the data returned by UIImageJPEGRepresentation is automatically freed, so it will hang until the current release pool is depleted (when you return to the main event loop). Think about adding:
NSAutoreleasePool* p = [[NSAutoreleasePool alloc] init];
at the top of the cycle, and
[p drain]
in the end. This way you will not skip all intermediate memory.
And finally, doing a linear search for the optimal compression setting is probably quite inefficient. Do a binary search instead.
source share