I receive a memory warning when using UIImageJPEGRepresentation, is there any way to avoid this? It doesn't crash the app but I'd like to avoid it if possible. It does intermittently not run the [[UIApplication sharedApplication] openURL:url];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
NSData *imageToUpload = UIImageJPEGRepresentation(image, 1.0);
// code that sends the image to a web service (omitted)
// on success from the service
// this sometime does not get run, I assume it has to do with the memory warning?
[[UIApplication sharedApplication] openURL:url];
}
Using
UIImageJPEGRepresentation
(in which you are round-tripping the asset through aUIImage
) can be problematic, because using acompressionQuality
of 1.0, the resultingNSData
can actually be considerably larger than the original file. (Plus, you're holding a second copy of the image in theUIImage
.)For example, I just picked a random image from my iPhone's photo library and the original asset was 1.5mb, but the
NSData
produced byUIImageJPEGRepresentation
with acompressionQuality
of 1.0 required 6.2mb. And holding the image inUIImage
, itself, might take even more memory (because if uncompressed, it can require, for example, four bytes per pixel).Instead, you can get the original asset using the
getBytes
method:This avoids staging the image in a
UIImage
and the resultingNSData
can be (for photos, anyway) considerably smaller. Note, this also has an advantage that it preserves the meta data associated with the image, too.By the way, while the above represents a significant memory improvement, you can probably see a more dramatic memory reduction opportunity: Specifically, rather than loading the entire asset into a
NSData
at one time, you can now stream the asset (subclassNSInputStream
to use thisgetBytes
routine to fetch bytes as they're needed, rather than loading the whole thing into memory at one time). There are some annoyances involved with this process (see BJ Homer's article on the topic), but if you're looking for dramatic reduction in the memory footprint, that's the way. There are a couple of approaches here (BJ's, using some staging file and streaming from that, etc.), but the key is that streaming can dramatically reduce your memory footprint.But by avoiding
UIImage
inUIImageJPEGRepresentation
(which avoids the memory taken up by the image as well as the largerNSData
thatUIImageJPEGRepresentation
yields), you might be able to make considerably headway. Also, you might want to make sure that you don't have redundant copies of this image data in memory at one time (e.g. don't load the image data into aNSData
, and then build a secondNSData
for theHTTPBody
... see if you can do it in one fell swoop). And if worst comes to worse, you can pursue streaming approaches.in ARC: Just put your code inside small block of @autoreleasepool
Presented as an answer for formatting and images.
Use instruments to check for leaks and memory loss due to retained but not leaked memory. The latter is unused memory that is still pointed to. Use Mark Generation (Heapshot) in the Allocations instrument on Instruments.
For HowTo use Heapshot to find memory creap, see: bbum blog
Basically the method is to run Instruments allocate tool, take a heapshot, run an iteration of your code and take another heapshot repeating 3 or 4 times. This will indicate memory that is allocated and not released during the iterations.
To figure out the results disclose to see the individual allocations.
If you need to see where retains, releases and autoreleases occur for an object use instruments:
Run in instruments, in Allocations set "Record reference counts" on (For Xcode 5 and lower you have to stop recording to set the option). Cause the app to run, stop recording, drill down and you will be able to see where all retains, releases and autoreleases occurred.