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: