Return to WrightsCS.com   |   Check out WrightsCSApps.com   |   How to Post Snippets  

Not registered? Thats okay! You can Submit a Snippet without being a member of this site!


*    64votes

Here is an easy way to use a sequence of images as a single animated UIImage. The concept behind this is to add your images in sequential order and return an animated image. This is has less math involved than actually using CoreAnimation and is only available in iOS 5.0 and beyond, you will need to use an alternative method in a previous version.

+ (UIImage *)animatedImageWithImages:(NSArray *)images duration:(NSTimeInterval)duration NS_AVAILABLE_IOS(5_0);

Here is an example of how you can add a sequence of images to an NSArray:

-(UIImage*)loadingImage
{
    NSArray *animationFrames = [NSArray arrayWithObjects:
                                [UIImage imageNamed:@"loading01.png"],
                                [UIImage imageNamed:@"loading02.png"],
                                [UIImage imageNamed:@"loading03.png"],
                                [UIImage imageNamed:@"loading04.png"],
                                [UIImage imageNamed:@"loading05.png"],
                                [UIImage imageNamed:@"loading06.png"],
                                [UIImage imageNamed:@"loading07.png"],
                                [UIImage imageNamed:@"loading08.png"],
                                [UIImage imageNamed:@"loading09.png"],
                                [UIImage imageNamed:@"loading10.png"],
                                [UIImage imageNamed:@"loading11.png"],
                                [UIImage imageNamed:@"loading12.png"],
                                [UIImage imageNamed:@"loading13.png"],
                                [UIImage imageNamed:@"loading14.png"],
                                [UIImage imageNamed:@"loading15.png"],
                                [UIImage imageNamed:@"loading16.png"],
                                nil];
    return [UIImage animatedImageWithImages:animationFrames duration:1.0f]; 
}

// Example using -(UIImage*)loadingImage
[self.myImage setImage: [self loadingImage]];

// Example using the above NSArray, animationFrames
[self.myImage setImage: [UIImage animatedImageWithImages:animationFrames duration:1.0]];

One Response to “Animated UIImage using NSArray”

  1. hello, nice tutorial , i wonder this code can be done by theos> in Tweak.xm ? i want to try it out, thanks !

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

© Copyright 2011 Wright's Computer Services.   All rights reserved Suffusion theme by Sayontan Sinha