我使用了由mayoff"(Rob Mayoff)UIImageView+animatedGIF"制作的类,这是在 stackoverflow 上的答案之一中提出的.UIImageView+animatedGIF
I used class made by "mayoff" (Rob Mayoff) "UIImageView+animatedGIF" which was proposed in one of the answers here on stackoverflow. UIImageView+animatedGIF
有了它,我可以在 iOS 上的 UIImageView 中导入动画 .gif 图像.这个类完美无缺,但唯一的问题是 .gif 总是循环播放.无论我如何导出它(我从 photoshop 导出图像 - 67 帧,将重复设置为一次"),它都会在 UIImageView 中永远循环.
With it I can import animated .gif images in UIImageView on iOS. This class works flawlessly, but the only problem is that .gif is always looping. No matter how I export it (I am exporting image from photoshop - 67 frames, set repeat to "once") it loops forever in UIImageView.
我正在用这两行导入我的 .gif:
I am importing my .gif with these two lines:
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Loading" withExtension:@"gif"];
self.loadingImageView.image = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]];
非常简单,而且很有效.我尝试按照 Rob 的建议将 animationRepeatCount 属性设置为 1,但这并没有成功.我也尝试将 UIImageView 的 animationImages 属性设置为gifImage.images 也是,而不仅仅是设置视图的图像属性到 gif 图像.在这种情况下,.gif 根本没有动画效果.
very simple and it works. I have tried setting animationRepeatCount property to 1, as recomended by Rob but that didn't do the trick. Also I have tried setting the UIImageView's animationImages property to gifImage.images too, instead of just setting the view's image property to gifImage. In that case, .gif is not animating at all.
知道如何只播放一次 .gif 吗?我能想到很多技巧(比如设置 gif 的最后一帧在一段时间后显示,但如果可能,我会先尝试一些更简单的方法).
Any ideas how to play that .gif only once? I can think of many tricks (like setting last frame of the gif to show up after some time but I'd first try something simpler if possible).
我从那个项目中取出了测试应用程序并将 viewDidLoad
方法更改为:
I took the test app from that project and changed the viewDidLoad
method to this:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"gif"];
UIImage *testImage = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]];
self.dataImageView.animationImages = testImage.images;
self.dataImageView.animationDuration = testImage.duration;
self.dataImageView.animationRepeatCount = 1;
self.dataImageView.image = testImage.images.lastObject;
[self.dataImageView startAnimating];
}
当应用启动时,它会为图像设置一次动画,然后仅显示最后一帧.如果我连接一个发送 [self.dataImageView startAnimating]
的按钮,然后点击该按钮再次运行动画,然后在最后一帧再次停止.
When the app launches, it animates the image once and then shows just the last frame. If I connect a button that sends [self.dataImageView startAnimating]
, then tapping the button runs the animation one more time, then stops again on the last frame.
确保按照我上面的方式设置图像视图.确保您不将图像视图的 image
属性设置为动画图像.如果要停止动画,则必须将图像视图的 image
属性设置为非动画图像.
Make sure you're setting your image view up the way I do above. Make sure you are not setting the image view's image
property to the animated image. The image view's image
property must be set to a non-animated image if you want the animation to stop.
这篇关于UIImageView+animatedGIF 总是 LOOPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!