0

I'm kind of stuck with this problem. I have a UIImageView animation with 37 images where I show a glass filling with alcohol. I initialize my image array in ViewDidLoad,

NSArray * GlassAnim  = [[NSArray alloc] initWithObjects:
                        [UIImage imageWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"SodaPour1" ofType:@"png"]],
                        [UIImage imageWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"SodaPour39" ofType:@"png"]],
                        nil];

imgGlass.animationImages = GlassAnim;
imgGlass.animationDuration = 2.5;
imgGlass.contentMode = UIViewContentModeScaleAspectFit;
imgGlass.animationRepeatCount=1;
[GlassAnim release];

Then I call startAnimating when user taps the screen. But the problem is that the animation has a half a second delay for the first time. Each image has a dimension of 330*372 pixels and the file size is 180KB png files. Is there a better way to do this other than playing a video?.

Thanks.

Damitha Raveendra
  • 1,721
  • 17
  • 24
  • One tip. You can use shorter form for an UIImage: `[UIImage imageNamed:@"SodaPour18.png"]` – Cyprian Jul 20 '11 at 13:50
  • No considerable change. Is there a way doing this using CALayer animations?.... – Damitha Raveendra Jul 21 '11 at 05:54
  • Mods don't answer questions at the request of users, so flagging for moderator attention isn't going to work. If you want some attention for your question, I would HIGHLY suggest you 1) [edit](http://stackoverflow.com/posts/6762408/edit) your question to make your code fit without causing the scrollbars to appear (you *seriously* don't need all those damn UIIMage lines; cut out most of them, shorten the remaining and move them to the left) and then 2) put a bounty on your question. You can read about bounties in the [faq#bounty]. Good luck. –  Jul 22 '11 at 12:40
  • Sorry for my lack of knowledge. I found the answer anyway. Thanks. – Damitha Raveendra Jul 25 '11 at 06:54
  • Can you share your answer deamonsarea? – adam Nov 29 '11 at 12:19
  • I'm looking for a solution for this as well. Can you please share it? – Dani Pralea Jan 25 '12 at 17:31
  • I Just posted my answer. Hope it will help you guys. – Damitha Raveendra Jan 26 '12 at 10:21

1 Answers1

0

Here you go,

  1. You start a Timer to change image names.

    timerAnimation = [[NSTimer scheduledTimerWithTimeInterval:0.10 target:self
    selector:@selector(changeImagesAccordingly) userInfo:nil repeats:YES] 
    retain];
    
  2. Rename the images in a sequence. I have 16 images.

    Eg:- "AnimationImg1.png", "AnimationImg2.png", "AnimationImg3.png"

    - (void)changeImagesAccordingly {
        if (iImgAnim<16) {
            imgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]
                   pathForResource:[NSString stringWithFormat:@"AnimationImg%d",iImgAnim] ofType:@"png"]];
            iImgAnim++;
        } else {
            [timerAnimation invalidate];
            iImgAnim=1;
        }
    }
    

That should do it :)

Damitha Raveendra
  • 1,721
  • 17
  • 24
  • 1
    This answer is an okay first step as it does not decode all the images into memory before animating. You will likely still have some performance issues as it looks like your are tying to get about 20FPS with rather large images. For more info on FPS and performance, see http://stackoverflow.com/questions/16880798/how-to-assemble-animation-from-an-array-of-images-with-the-highest-fps-possible/17128678#17128678 – MoDJ Jul 01 '13 at 09:09
  • Thanks for the tip MoDJ. In my case I was able to solve it. But your answer should be the right one I guess. – Damitha Raveendra Jul 01 '13 at 09:33
  • Your approach is good enough as a first shot at it. Note that you can find my own impl of the same sort of timer based animation in this answer http://stackoverflow.com/questions/442076/method-for-animating-images-like-a-movie-on-iphone-without-using-mpmovieplayer/6077394#6077394 – MoDJ Jul 01 '13 at 09:42