是否有可能为UIImage制作动画?

时间:2011-12-17 13:43:49

标签: iphone objective-c xcode ios5 uiimage

我想知道是否有动画UIImage

我知道UIImageViews可以制作动画,但是有没有办法直接在UIImage中进行动画制作。

也许使用Open GL ES或其他什么?

或者还有其他方法可以动画MPMediaItemArtwork吗?

提前致谢!

5 个答案:

答案 0 :(得分:13)

创建UIImageView并将animationImages的属性设置为UIImage的数组

以下是一个例子:

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

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

如果您的目标是iOS 5,则可以直接在UIImage中执行此操作而不使用UIImageView

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

例如,

    [UIImage animatedImagesWithImages:animationFrames duration:10];

答案 1 :(得分:8)

如果您的意思是包含少量图片的动画,请使用以下代码:

   // 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

答案 2 :(得分:1)

检查UIImage的+animatedImageWithImages:duration:+animatedImageNamed:duration:

来自documentation

  

从现有图像集创建并返回动画图像。

     

此方法通过将一系列数字附加到name参数中提供的基本文件名来加载一系列文件。例如,如果name参数以'image'作为其内容,则此方法将尝试从名称为'image0','image1'等文件加载到'image1024'的文件中加载图像。动画图像中包含的所有图像应具有相同的大小和比例。

答案 3 :(得分:0)

你可以使用它。

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"];

答案 4 :(得分:0)

Swift 4版本:

let animationImages = [UIImage(named: "image1.png"), UIImage(named: "image2.png")]
imageView.animationImages = animationImages
imageView.animationDuration = 10
imageView.startAnimating()