0

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:

imgArray = [[NSMutableArray alloc] init];
for(int i = 0; i <= 30; i++) {
    NSString * filename = [NSString stringWithFormat:@"img_000%.2d.png", i];
    UIImage *temp = [UIImage imageNamed:filename];
    [imgArray addObject:temp];
    [temp release];
    temp = nil;
} 

I use the following codes for displaying the images:

CGImageRef image = [(UIImage *)[imgArray objectAtIndex:imgFrame] CGImage];
imgLayer.contents = (id)image;
if(imgFrame < 29) {
    imgFrame++;
} else {
    imgFrame = 0;
    imgLayer.hidden = TRUE;
    [imgTimer invalidate];
}

where imgLayer is a CALayer. (imgTimer is a repeating timer with interval 0.03s)

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.

Is it related to preloading images? Or are my images too big in file size?

Raptor
  • 53,206
  • 45
  • 230
  • 366
  • I don't think this will help your particular problem, but it might not be a bad idea to move your variable declarations outside of the for loop. I don't know if the gcc is smart enough to fix this, but it's rather painful to look at . – KevinDTimm Dec 09 '09 at 08:50

2 Answers2

0

The reason for your lags are hard to tell without profiling data. But here is a trick that might help: Join all your images into one large file. Try to make it rectangular (maybe 6x6 in your case or 4*8). Then load this single file and crop each image out for display (i.e. create an image for display with the size of a tile and copy one tile from the big image after the other into the display image).

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Just like a Sprite sheet ? Okay, I will try. However, my PNGs are all in 480 x 500 px size; if I join them all, it would be a huge image, which I don't think iPhone can handle. Maybe I should make my images' dimension smaller. – Raptor Dec 09 '09 at 08:54
  • Yeah, like a Sprite sheet. Also, if you need to animate them, consider to use an animated format (like AVI or *shudder* anim GIF or MNG). The iPhone can play videos at full display resolution without problems. – Aaron Digulla Dec 09 '09 at 09:49
  • The sprite sheet idea is reasonable but you're answering to another question. And loading the bigger sprite file shouldn't take that much shorter time. – Cyril Godefroy Feb 10 '12 at 09:16
  • Eventually, the sprite box idea was the really best idea. Using the UIView animation extensions or the UIImage images array to display a series of images is easy but underperforming. Using the sprite is much much better. But it's not as easy as "sprite sheet". – Cyril Godefroy Apr 10 '12 at 11:50
0

Images are loaded when they are used and displayed. If you use the time profiler in Instruments, you will the lag you're experiencing. If you then zoom to that lag and look at what is causing it, you will usually see that "copyImageBlockSetPNG" is the function taking time, right before "inflate".

What you must find a way to do is create your images and force load them before you need them. That's another story apparently.

Cyril Godefroy
  • 1,400
  • 12
  • 15