title: Memory problems with displaying large images in UITableView
body: <p>I have a system which loads alot of large images from the web and displays them in custom table cells. On older devices the memory warnings happen pretty quickly so I implemented a system of deleting some from the table to try to combat this but it didn't work well enough (lots of images were deleted affecting the UI). </p>
  
  <p>So I thought I could load all the images into the device's cache and then load them from there - I've implemented <a href="https://github.com/rs/SDWebImage" rel="nofollow noreferrer">SDWebImage</a>. This is great but I still havent solved the problem of memory allocation as the images are still being displayed all the time and therefore kept in memory - causing crashes. </p>
  
  <p>I think I need to implement a system which shows the images (from the cache) if the cell is being displayed and hide it if the cell is not showing - I'm just stuck at how to build such a system. </p>
  
  <p>Or is this not going to work? Can you really keep the apps memory low (and stop it having memory warnings / crashing) by removing images from its table cells? Or do I just need to carry on with my earlier solution and just delete images/cells until the memory warnings stop?</p>
  
  <p><strong>Updated with code</strong></p>
  
  <p>TableViewController.m</p>
  
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
  {   
      if (indexPath.section == 0)
      {
      currentIndexPath = indexPath;
  
      ImageTableCell *cell = (ImageTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  
      ImageDownloader *download = [totalDownloads objectAtIndex:[indexPath row]];
  
      if (cell == nil) 
      {
          cell = [[[ImageTableCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
      }
  
      cell.imageView.image = download.image;
  
      return cell;
  }
  return nil;
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
  {   
      int t = [totalDownloads count];   
      return t;
  }
  </code></pre>
  
  <p>ImageTableCell.m - Custom cell</p>
  
  <pre><code>- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  {
  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  if (self) 
  {
      self.frame = CGRectMake(0.0f, 0.0f, 320.0f, 0.0f);
      self.contentView.frame = CGRectMake(0.0f, 0.0f, 320.0f, 0.0f);
  
      self.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
      self.contentMode = UIViewContentModeScaleToFill;
      self.autoresizesSubviews = YES;
  
      self.contentView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
      self.contentView.contentMode = UIViewContentModeScaleToFill;
      self.contentView.autoresizesSubviews = YES;
  
      [self.imageView drawRect:CGRectMake(0.0f, 0.0f, 320.0f, 0.0f)];
      self.imageView.contentMode = UIViewContentModeScaleAspectFill;
      self.imageView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
      self.imageView.opaque = YES;
  }
  return self;
  }
  </code></pre>
  
  <p>ImageDownloader (implements SDWebImageManagerDelegate)</p>
  
  <pre><code>    -(void) downloadImage // Comes from Model class
      {    
      if (image == nil)
      {
          NSURL *url = [NSURL URLWithString:self.urlString];
  
          SDWebImageManager *manager = [SDWebImageManager sharedManager];
  
          // Remove in progress downloader from queue
          [manager cancelForDelegate:self];
  
          if (url)
          {   
              [manager downloadWithURL:url delegate:self retryFailed:YES];
          }
      }
      }
  
      - (void)cancelCurrentImageLoad
      {
          [[SDWebImageManager sharedManager] cancelForDelegate:self];
      }
  
      - (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)_image
      {      
          self.image = _image;
  
          if ([self.delegate respondsToSelector:@selector(addImageToModel:)]) [self.delegate addImageToModel:self];
      }
      - (void)webImageManager:(SDWebImageManager *)imageManager didFailWithError:(NSError *)error;
      {
          if ([self.delegate respondsToSelector:@selector(badImage)]) [self.delegate badImage];
      }
  </code></pre>