views:

151

answers:

2

I'm using UIImageView to run a flipbook anim like this:

mIntroAnimFrame = [[UIImageView alloc] initWithFrame:CGRectMake( 0, 0, 480, 320);
mIntroAnimFrame.image = [UIImage imageNamed:@"frame0000.tif"];

Basically, when determine it is time, I flip the image by just calling:

mIntroAnimFrame.image = [UIImage imageNamed:@"frame0000.tif"];

again with the right frame. Should I be doing this differently? Are repeated calls to set the image this way possibly bogging down main memory, or does each call essentially free the previous UIImage because nothing else references it? I suspect the latter is true.

Also, is there an easy way to preload the images? The anim seems to slow down at times. Should I simply load all the images into a dummy UIImage array so they are preloaded, then refer to it when I want to show it with mIntroAnimFrame.image = mPreloadedArray[i]; ?

+1  A: 

Hi

I was typing up an example but remembered there was a perfect one at http://www.iphoneexamples.com/

NSArray *myImages = [NSArray arrayWithObjects:
    [UIImage imageNamed:@"myImage1.png"],
    [UIImage imageNamed:@"myImage2.png"],
    [UIImage imageNamed:@"myImage3.png"],
    [UIImage imageNamed:@"myImage4.gif"],
    nil];

UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:[self bounds]];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 0.25; // seconds
myAnimatedView.animationRepeatCount = 0; // 0 = loops forever
[myAnimatedView startAnimating];
[self addSubview:myAnimatedView];
[myAnimatedView release]; 

Was this what you were thinking of?

RickiG
Worked beautifully. Thanks to all who responded.
Joey
A: 

[UIImage imageNamed:] will cache images, so they will not be immediately freed. There is nothing wrong with that, but if you know you will not use the image again for a while, use a different method to get the images.

UIImageView also has built in animation abilities if you have a regular frame rate.

mIntroAnimFrame.animationImages = mPreloadedArray;
[mIntroAnimFrame startAnimating];
drawnonward