如何在我的iPhone应用程序中使用动态的.gif文件?

20

我有一个动画gif文件,想在我的iPhone应用程序中使用,但是动画不起作用。有人知道如何修复它吗?

5个回答

27

如果你有一系列的图片需要进行动画,你可以使用 UIImageView 轻松实现:

UIImage *blur5 = [UIImage imageNamed:@"blur5.png"];
UIImage *blur6 = [UIImage imageNamed:@"blur6.png"];

self.imageView.animationImages = [[NSArray alloc] initWithObjects:blur5, blur6, nil];
self.imageView.animationRepeatCount = 5;
[self.imageView startAnimating];

我发现这比尝试使用UIWebView更容易。


18
这是演示图像动画的正确方法。但这不是问题的正确答案。 - Satyam

7

5
您可以使用这个链接中提供的源代码。它有一个GIF解码器可以直接使用,以得到解决方案。我已经成功地使用了它。但是,它在透明度方面存在一些问题。

3
这可以通过以下代码实现:
NSArray * imageArray  = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"1.png"], [UIImage imageNamed:@"2.png"], nil]; //this will be the frames of animation images in sequence.
 ringImage = [[UIImageView alloc]initWithFrame:CGRectMake(100,200,600,600)];
 ringImage.animationImages = imageArray;
 ringImage.animationDuration = 1.5;//this the animating speed which you can modify
 ringImage.contentMode = UIViewContentModeScaleAspectFill;
 [ringImage startAnimating];//this method actually does the work of animating your frames.

我知道这是一个老帖子,但可能对某些人有所帮助。 :)

1

另一个选择是在您的应用程序中解码gif,然后将其“帧服务”到OpenGL对象。这种方式不太可能因为大型gif而耗尽内存。


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