objective c - iOS 7 App Terminated due to Memory Pressure

I have a two views of an ipad App which utilizes large UIScrollowView's, which appears to be leading to memory issues. I get my first memory warning when memory hits about 250MB. When using Apple Instruments, here are the largest of the All Heap:

VM: ImageIO_JPEG_Data, size 32.13MB VM: CoreAnimation, size 32.13MB VM: ImageIO_JPEG_Data, size 26.64MB ...

I have many images within the scrollview - which are all loaded into the storyboard, and I'm wondering if I reduce the size or resolution of the images it will fix the issue?

2 Answers

  1. Michael- Reply


    You could set up a system to delete/load certain images at certain points in the scrollview, so the app only has them in memory when the user is about to see them.

    Because you said it's rather large, in the scrollview's scroll method, set variables to know what position the scrollview is in (eg: half way down, 10% scrolled, etc). If the user is sitting at the end of the scrollview (ie: unable to scroll down anymore (100%)) then they're not exactly looking at the images at the top of the view. You could then nil these images so that the app can re-use that memory. When the user is scrolling and the scrollview is near a certain percentage, you can then load those images that the user is about to see and nil the images that the user is far away from (this might cause a few seconds of 'whitespace' when the user is scrolling madly through your app, but that's okay because it won't take more than a few seconds for the images to all load).

    Because these images are loaded into storyboard, I'm assuming you're loading them via UIImageView's. Simply give each one an outlet (hold control + click and drag it to your class's .h file and create an IBOutlet), then when you have the percentage of how far the user is down your scrollview you can know 'okay, the user is at 50% so I can get rid of the images from 0-20% and 80-100% of the scrollview', you do this by:

    //^Get scrollview scrolled percentage
    //^find out where the user is, get a list of UIImageViews that are not visible on the screen  
    _yourUIImageView.image = nil //get rid of the image, clear memory up
    //then when you need to add it again, assign an image to the UIImageview

    Hope I helped.

  2. Mike- Reply


    Maximum do not load images in Storyboard, rather you can do it through coding,like below

    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    NSString *pathForImageFile1 = [resourcePath stringByAppendingPathComponent:@"filename.png"];
    UIImage * image = [[UIImage alloc] initWithContentsOfFile:pathForImageFile1];
    imageView.image = image;
    image = nil;

    Hope it helps!

Leave a Reply

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

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>