Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading new images into UIImageView, memory from previous images is not released
    text
    copied!<p>My app has a view with three <code>UIImageViews</code>. There are about 150 images as resource files, each a .jpg of about 24k. The app will show all of the images, but only three at a time.</p> <p>But every time I click the button to load three more images into the <code>UIImageViews</code>, the "Overall Bytes" column in Instruments increases by about 700K, until I eventually get a low memory warning and then the app crashes. Somehow, the memory I use for each load of three images isn't being released.</p> <p>I've googled around and tried every way I know to do this, but no success and I need help.</p> <p>I've tried this: (This code is part of the action to load three more images. The three ImageViews are <code>imgShownLeft</code>, <code>imgShownMiddle</code>, and <code>imgShownRight</code>. Those are instance variables. I first have to put <code>currentImage</code> in all three <code>UIImageViews</code>, and then put <code>previousImage</code> in the left one and <code>nextImage</code> in the right one:</p> <pre><code>NSString *strImageToSet = [[imgNamesArray objectAtIndex:currentImage] stringByAppendingFormat:@".jpg"]; UIImage *img = [UIImage imageNamed:strImageToSet]; [imgShownMiddle setImage:img]; [imgShownLeft setImage:img]; [imgShownRight setImage:img]; NSString *strPreviousImage = [[imgNamesArray objectAtIndex:previousImage] stringByAppendingFormat:@".jpg"]; img = [UIImage imageNamed:strPreviousImage]; [imgShownLeft setImage:img]; NSString *strNextImage = [[imgNamesArray objectAtIndex:nextImage] stringByAppendingFormat:@".jpg"]; img = [UIImage imageNamed:strNextImage]; [imgShownRight setImage:img]; </code></pre> <p>Then I tried this: instead of:</p> <pre><code> UIImage *img = [UIImage imageNamed:strImageToSet]; </code></pre> <p>I used:</p> <pre><code> UIImage *img = [[UIImage alloc] initWithContentsOfFile:strImageToSet]; </code></pre> <p>and assigned <code>img</code> to the <code>UIImageViews</code> the same way, followed by <code>[img release];</code></p> <p>Then I tried:</p> <pre><code> UIImage *img = [[[UIImage alloc] initWithContentsOfFile:strImageToSet] autorelease]; </code></pre> <p>Then I tried:</p> <pre><code> img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[imgNamesArray objectAtIndex:currentImage] ofType:@"jpg"]]; </code></pre> <p>But in every case the memory requirements grew with each run of this action.</p> <p>I know it's possible to keep three images loaded at a time without holding 150 images in memory! What am I doing wrong?</p> <p>Thanks for any insight. /SD</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload