title: Preload Images in iPhone application?
body: <p>I have a sequence of images needed to display in a short time (PNG sequence). There are totally 31 PNGs in total, each with file size about 45KB. I have already loaded them with the following codes:</p>
  
  <pre><code>imgArray = [[NSMutableArray alloc] init];
  for(int i = 0; i &lt;= 30; i++) {
      NSString * filename = [NSString stringWithFormat:@"img_000%.2d.png", i];
      UIImage *temp = [UIImage imageNamed:filename];
      [imgArray addObject:temp];
      [temp release];
      temp = nil;
  } 
  </code></pre>
  
  <p>I use the following codes for displaying the images:</p>
  
  <pre><code>CGImageRef image = [(UIImage *)[imgArray objectAtIndex:imgFrame] CGImage];
  imgLayer.contents = (id)image;
  if(imgFrame &lt; 29) {
      imgFrame++;
  } else {
      imgFrame = 0;
      imgLayer.hidden = TRUE;
      [imgTimer invalidate];
  }
  </code></pre>
  
  <p>where imgLayer is a <code>CALayer</code>. (imgTimer is a repeating timer with interval 0.03s)</p>
  
  <p>But I found that when I call the images out, it is very laggy at the first time. Except the 1st appearance, other appearance has no problem.</p>
  
  <p>Is it related to preloading images? Or are my images too big in file size?</p>