title: UIButton with background image from sprite sheet in table view causes memory warning?
body: <p>I have a sprite sheet in my documents directory. The sprites sheet consists of 256 icons. </p>
  
  <p>I have a table view, in which for each row I am creating 3 buttons with background images from the sliced part of the sprite sheet. </p>
  
  <p>Below is the code to extract the sliced icon from sprite. Using the coordinates known </p>
  
  <pre><code>    -(UIImage *)getSlicedImageForDictionary:(NSDictionary *)spriteInfoDictionary spriteImageRef:(UIImage *)spriteImageReference{
  
      NSString *frameString = [spriteInfoDictionary objectForKey:@"frame"];
      NSString *frameStringWithOnlyCommas = [[[frameString stringByReplacingOccurrencesOfString:@"\"" withString:@""] stringByReplacingOccurrencesOfString:@"{" withString:@""] stringByReplacingOccurrencesOfString:@"}" withString:@""];
  
      NSArray *frameValues = [frameStringWithOnlyCommas componentsSeparatedByString:@","];
  
      CGImageRef cgIcon = CGImageCreateWithImageInRect(spriteImageReference.CGImage, CGRectMake([[frameValues objectAtIndex:0] floatValue],     [[frameValues objectAtIndex:1] floatValue], [[frameValues objectAtIndex:2] floatValue], [[frameValues objectAtIndex:3] floatValue]));
      UIImage *icon = [UIImage imageWithCGImage:cgIcon];
      frameString = nil;
      frameStringWithOnlyCommas = nil;
      frameValues = nil;
      CGImageRelease(cgIcon);
      return icon;
  }
  </code></pre>
  
  <p>I am calling the above method in the for loop as below,</p>
  
  <pre><code>for (int i = 0; i &lt; [iconInfoArray count]; i ++) {   
     NSDictionary *currentBook = [spriteInfoDictionary objectForKey:[iconInfoArray objectAtIndex:i]];
     articleMetaData.articleImage = [self getSlicedImageForDictionary:currentBook spriteImageRef:self.spriteImageSource];
     [spriteIconsArray addObject:articleMetaData];
  }
  </code></pre>
  
  <p><strong>The problem is,</strong>
  When I set the buttons in the table view from the spriteIconsArray I am getting the "Received memory warning" problem and the application crashes. </p>
  
  <pre><code>[btn setBackgroundImage:[spriteIconsArray objectAtIndex:i] forState:UIControlStateNormal];
  </code></pre>
  
  <p><strong>But</strong> when I write the image reference to the documents directory I am not getting any memory warnings and the application runs smoothly. </p>
  
  <pre><code>NSData *data = UIImagePNGRepresentation([spriteIconsArray objectAtIndex:i]);
  NSString *pathOfTempImage = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"tempIcon.png"];
  [data writeToFile:pathOfTempImage atomically:YES];
  [btn setBackgroundImage:pathOfTempImage forState:UIControlStateNormal];
  </code></pre>
  
  <p><strong>I am confused why this is happening. I don't want to write the image to the documents and read it again, which may create some latency for large set of data.</strong></p>
  
  <p>Please suggest me in this.</p>
  
  <p>Thanks</p>