UIImageView+animatedGIF 总是循环

2024-01-24

我使用了“mayoff”(Rob Mayoff)“UIImageView+animatedGIF”制作的类,这是在 stackoverflow 上的答案之一中提出的。UIImageView+动画GIF https://github.com/mayoff/uiimage-from-animated-gif

有了它,我可以在 iOS 上的 UIImageView 中导入动画 .gif 图像。这个类工作完美,但唯一的问题是 .gif 总是循环。无论我如何导出它(我从 Photoshop 导出图像 - 67 帧,将重复设置为“一次”),它都会在 UIImageView 中永远循环。

我用这两行导入 .gif:

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 根本不具有动画效果。

有什么想法如何只播放该 .gif 一次吗?我能想到很多技巧(比如设置 gif 的最后一帧在一段时间后显示,但如果可能的话我会先尝试一些更简单的方法)。


我从该项目中获取了测试应用程序并更改了viewDidLoad方法:

- (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],然后点击按钮再次运行动画,然后在最后一帧再次停止。

确保您按照我上面的方式设置图像视图。确保你是not设置图像视图的image动画图像的属性。图像视图的image如果您希望动画停止,则必须将属性设置为非动画图像。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UIImageView+animatedGIF 总是循环 的相关文章

随机推荐