10

I'm wondering if there's anyway to animate an UIImage.

I know that UIImageViews are possible to animate but is there any way to do it directly in a UIImage.

Maybe with Open GL ES or something?

Or are there any other ways you can animate an MPMediaItemArtwork?

Thanks in advance!

Mutix
  • 4,196
  • 1
  • 27
  • 39
Oskar Larsson
  • 155
  • 1
  • 1
  • 7

5 Answers5

13

Create a UIImageView and set the property of animationImages to an array of UIImages

Here is an example:

NSArray *animationFrames = [NSArray arrayWithObjects:
  [UIImage imageWithName:@"image1.png"],
  [UIImage imageWithName:@"image2.png"], 
  nil];

UIImageView *animatedImageView = [[UIImageView alloc] init];
animatedImageView.animationImages = animationsFrame;
[animatedImageView startAnimating];

If you're targeting iOS 5 you can do this directly in UIImage without the UIImageView using

    +(UIImage *)animatedImageWithImages:(NSArray *)images duration:(NSTimeInterval)duration 

for example,

    [UIImage animatedImagesWithImages:animationFrames duration:10];
Pang
  • 9,564
  • 146
  • 81
  • 122
richerd
  • 1,247
  • 11
  • 10
8

If you mean animation with a few images, use this code:

   // create the view that will execute our animation
 UIImageView* YourImageView = [[UIImageView alloc] initWithFrame:self.view.frame];

 // load all the frames of our animation
 YourImageView.animationImages = [NSArray arrayWithObjects:    
                             [UIImage imageNamed:@"01.gif"],
                             [UIImage imageNamed:@"02.gif"],
                             [UIImage imageNamed:@"03.gif"],
                             [UIImage imageNamed:@"04.gif"],
                             [UIImage imageNamed:@"05.gif"],
                             [UIImage imageNamed:@"06.gif"],
                             [UIImage imageNamed:@"07.gif"],
                             [UIImage imageNamed:@"08.gif"],
                             [UIImage imageNamed:@"09.gif"],
                             [UIImage imageNamed:@"10.gif"],
                             [UIImage imageNamed:@"11.gif"],
                             [UIImage imageNamed:@"12.gif"],
                             [UIImage imageNamed:@"13.gif"],
                             [UIImage imageNamed:@"14.gif"],
                             [UIImage imageNamed:@"15.gif"],
                             [UIImage imageNamed:@"16.gif"],
                             [UIImage imageNamed:@"17.gif"], nil];

 // all frames will execute in 1.75 seconds
 YourImageView.animationDuration = 1.75;
 // repeat the animation forever
 YourImageView.animationRepeatCount = 0;
 // start animating
 [YourImageView startAnimating];
 // add the animation view to the main window 
 [self.view addSubview:YourImageView];

Source

1

Check UIImage's +animatedImageWithImages:duration: and +animatedImageNamed:duration:.

From UIImage Class Reference:

Creates and returns an animated image from an existing set of images.

This method loads a series of files by appending a series of numbers to the base file name provided in the name parameter. For example, if the name parameter had ‘image’ as its contents, this method would attempt to load images from files with the names ‘image0’, ‘image1’ and so on all the way up to ‘image1024’. All images included in the animated image should share the same size and scale.

Community
  • 1
  • 1
KPM
  • 10,558
  • 3
  • 45
  • 66
0

You can use this.

arrayOfImages=[[NSMutableArray alloc]init];
for(int i=1;i<=54;i++)
{
NSString *nameResources=@"haKsequence.png";
NSString *changedString= [NSString stringWithFormat:@"%d", i];
NSString *stringWithoutSpaces = [nameResources     stringByReplacingOccurrencesOfString:@"K" withString:changedString];
[arrayOfImages addObject:(id)[UIImage imageNamed:stringWithoutSpaces].CGImage];
}


self.view.userInteractionEnabled=NO;
i1.hidden=YES;
image1=[[UIImageView alloc]init];
image1.backgroundColor=[UIColor clearColor];
image1.frame = button.frame;//i1 is obshaped buttion
[self.view addSubview:image1];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
animation.calculationMode = kCAAnimationDiscrete;
animation.duration = 1;
animation.values   = arrayOfMonkeyImages;
[animation setDelegate:self];
animation.repeatCount=3;
[animation setRemovedOnCompletion:YES];
[image1.layer addAnimation:animation forKey:@"animation"];
Ben Collier
  • 618
  • 4
  • 13
Sishu
  • 1,510
  • 1
  • 21
  • 48
0

Swift 4 version:

let animationImages = [UIImage(named: "image1.png"), UIImage(named: "image2.png")]
imageView.animationImages = animationImages
imageView.animationDuration = 10
imageView.startAnimating()
Fogh
  • 1,275
  • 1
  • 21
  • 29