views:

181

answers:

1

Hi,

What kind of trick can I use to speed up and improve an image sequence animation?

I'm using the code below, but then just running through the codes below takes a couple of seconds to finish.

Once the animations have been created, my phone seems to get much slower too.

Please enlight.

Thanks, Tee

    NSMutableArray * sequenceArray;
sequenceArray = [[NSMutableArray alloc] init];

int k;
int xPos = 0;
int yPos = 50;
for (k = 1; k <= 48; k++)
{
 UIImageView* dynamicView = [[UIImageView alloc] initWithFrame:self.view.frame];
 [sequenceArray addObject:dynamicView];

 int i;
 NSMutableArray *a = [NSMutableArray array];
 for (i = 1; i <= 102; i++)
 {
  [a addObject:[UIImage imageNamed:[NSString stringWithFormat:@"loop2 %d.png", i]]];
 }

 dynamicView.animationImages = a;
 //[a release];

 // all frames will execute in 1.75 seconds
 dynamicView.animationDuration = 1.6;
 // repeat the annimation forever
 dynamicView.animationRepeatCount = 0;


 CGRect frame1 = dynamicView.frame;
 frame1.size.width = 34;
 frame1.size.height = 34;
 frame1.origin.x = xPos;
 frame1.origin.y = yPos;
 dynamicView.frame = frame1;

 if (k % 8 == 0) {
  xPos = 0;
  yPos += 40;
 }

 xPos += 40;
 NSLog(@"step last");
 // start animating
 [dynamicView startAnimating];
 // add the animation view to the main window 
 [self.view addSubview:dynamicView];
 [dynamicView release];
}
+3  A: 

So, you're creating 48 UIImageViews, each with an animation of the same 102 frames? Am I reading that right?

The first thing that occurs to me is to pull this out of the other loop, since it's loading the same array each time, and just assign it to each view:

    int i;
    NSMutableArray *a = [NSMutableArray array];
    for (i = 1; i <= 102; i++)
    {
            [a addObject:[UIImage imageNamed:[NSString stringWithFormat:@"loop2 %d.png", i]]];
    }

But, really, why are you doing this?

Sixten Otto
Thanks for the suggestion. Putting that loop outside helps a little but it's still slow. I'm making a Christmas tree decoration app. That is for the Christmas lights. Is there an easier / more optimized way to do what I'm trying to do?Thanks, Tee
teepusink
You'd have to do your own timing code, but I imagine it'd probably be *much* faster to have one view with the tree as its background, and then draw each frame of the animation yourself. In each frame, blit one of your 102 images to each of the 48 spots. Having to composite 48 UIImageViews is a lot of work for the OS. (And it may well be maintaining 48 animation timers, too....)
Sixten Otto
Thanks Otto. I think I'm going to look into cocos2d and see if there is anything I might be able to use.Thanks, Tee
teepusink