能否为UIImage添加动画效果?

10
我想知道是否有办法对UIImage进行动画处理。我知道可以用UIImageViews来实现动画,但有没有直接在UIImage上进行动画的方法呢?也许可以使用Open GL ES或其他方式吗?或者还有其他方法可以对MPMediaItemArtwork进行动画处理吗?谢谢!

你需要什么类型的动画?简单的平移和移动?还是更改显示? - richerd
基本上是在多个帧之间切换,就像 .gif 一样。比如先显示图像1,然后显示图像2,再显示图像3等等。 - Oskar Larsson
5个回答

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

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


1

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

来自UIImage类参考

从现有的一系列图像创建并返回一个动画图像。

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


0

Swift 4 版本:

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接