title: UIScrollView with many UIImageViews - Memory management
body: <p>I have this huge problem with memory management.</p>
  
  <p><strong>The problem</strong>:<br>
  I've got a <code>UIScrollView</code>, I've got an Array with 24 paths to Images in it and I want to show them in the <code>UIScrollView</code> with paging enabled.</p>
  
  <p>All images is in the size 1024x748 (iPad landscape resolution with status bar) and the filetype is jpg or png.</p>
  
  <p>I'm using <em>lazy loading</em> just to not exceed the memory when the <code>viewDidLoad</code>. And I'm going with the lazy load sample from Apple with the PageControl. Though I'm using <code>UIImageViews</code> instead of <code>UIViews</code>.</p>
  
  <p>So my problem is that when I scroll to the third image, i want to remove the first image from the <code>UIScrollView</code> and release its memory. Because the further I scroll, more memory is draining. When I page in the UIScrollView and a new image is loaded and added, about 5000kb more memory is used, and when i step in to the <code>unloadPage:</code> (see below) nothing is released. Am I just "doing it wrong"?</p>
  
  <p>How do I release and remove <code>UIImageViews</code> properly?</p>
  
  <p>(I'm loading the UIImages with <code>initWithContentsOfFile:</code>)</p>
  
  <p><strong>Here is my code</strong>:  </p>
  
  <pre><code>@interface SlideViewController : UIViewControllerExtended &lt;UIScrollViewDelegate&gt; {
  
      ScrollViewController *slider;
      IconView *currentChapter;
      NSMutableArray *chapters;
      NSMutableArray *views;
      UIImageView *controller;
  
  }
  </code></pre>
  
  <p><strong>The lazy load function</strong>:</p>
  
  <pre><code>- (void) loadImageToScrollView:(NSInteger)chapter withPage:(NSInteger)page {
  
  
      if (page &lt; 0) return;
      if (page &gt;= chapterCount) return;
  
      if([views objectAtIndex:page] != [NSNull null]) return;
  
      NSMutableArray *all = [[currentChapter getImages] copy];
  
      if(!([[all objectAtIndex:page] rangeOfString:@".mp4"].length &gt; 0)) {
          controller = [views objectAtIndex:page];
  
          if((NSNull *)controller == [NSNull null]) {
  
              NSArray *paths = [[all objectAtIndex:page] componentsSeparatedByString:@"."];
  
              NSString *name = [[NSString alloc] initWithString:[paths objectAtIndex:0]];
              NSString *ending = [[NSString alloc] initWithString:[paths objectAtIndex:1]];
  
              NSString *file = [[NSString alloc] initWithFormat:@"%@", [[NSBundle mainBundle] pathForResource:name ofType:ending]];
  
              UIImageView *tempImage = [[UIImageView alloc] initWithFrame:CGRectMake(page * 1024, 0, 1024, 768)];
              UIImage *img = nil;
              img = [[UIImage alloc] initWithContentsOfFile:file];
  
              [tempImage setImage:img];
              [tempImage setTag:page];
  
              self.controller = tempImage;
              [tempImage release];
  
              [slider addSubview:controller];
              [views replaceObjectAtIndex:page withObject:controller];
  
              [name release];
              [ending release];
              [file release];
          }
  
      }
  
      [all release];
  }
  </code></pre>
  
  <p><strong>The unload view method</strong> (which does not seem to work):  </p>
  
  <pre><code>- (void) unloadPage: (int) page {
      if(page &lt; 0) return;
      if(page &gt;= chapterCount) return;
      if((NSNull *)[views objectAtIndex:page] != [NSNull null]) {
          UIImageView *viewToDelete = [views objectAtIndex:page];
  
          [viewToDelete removeFromSuperview];
  
          [views replaceObjectAtIndex:page withObject:[NSNull null]];
      }
  }
  </code></pre>