I ran into another interesting memory issue while working on the Photobooth app. This time I was testing a template that took 4 photos, and right as the photos finished and I pushed the images to the print view controller, the app crashed. The console message said something about memory issues, so I immediately booted up trusty ol’ Instruments, and reran.

Turns out, when the print view controller is loading, my app was trying to allocate a whopping 1.26 to 1.5 GB of memory. What!!


Digging in into the Call Tree, the culprit was UIGraphicsBeginImageContextWithOptions, and its submethod CGBitmapContextCreate.

Looking through my code and commenting things out and rerunning, this was the innocuous line that caused all the trouble:


UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);

The key part is the scale. Setting a scale of 0 is to use the default phone scale. In my case, I was testing with an iPhone 6+, so that means 3x. The function calling this creates the final image that gets printed, and I was using the same resolution as the original camera image, so about 4860 x 7025. At 3x, that’s:

4860px * 7025px * 3 * 1 byte/pixel = 102,424,500 bytes ~ 100 mb

which is apparently blown by up by 10x to create the image context. I don’t really understand what’s happening underneath here.

Since this wasn’t an image I was displaying at full resolution on the phone though, 3x scale is unnecessary so I changed it to 1x, and memory usage became a lot more reasonable, about 100 mb.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *