【问题标题】:UIImageView+animatedGIF always LOOPSUIImageView+animatedGIF 总是 LOOPS
【发布时间】:2013-07-25 17:01:20
【问题描述】:

我使用了由“mayoff”(Rob Mayoff)“UIImageView+animatedGIF”制作的类,这是在 stackoverflow 上的答案之一中提出的。 UIImageView+animatedGIF

有了它,我可以在 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 的最后一帧在一段时间后显示,但如果可能的话我会先尝试一些更简单的方法)。

【问题讨论】:

    标签: ios animation uiimageview gif


    【解决方案1】:

    我从该项目中获取了测试应用程序并将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] 的按钮,然后点击该按钮再次运行动画,然后在最后一帧再次停止。

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

    【讨论】:

    • Rob,你能在地图视图覆盖stackoverflow.com/questions/20558591/… 中使用UIImage+animatedGIF 查看我的问题吗?我无法让您的类别为叠加层中的 gif 设置动画。
    • 哟,罗伯!感谢这个伟大的类别。我得到了 gif 动画以及设备上的所有内容,但是在 Xcode 6.3 中调试时出现大量链接器错误。示例项目的目标是 iOS 5...在当前版本的 Xcode 中,您是否可以在不将架构更改为 32 位的情况下获得干净的构建?
    • @HimanshuGarg 听起来你应该发布一个新问题,解释你尝试了什么、发生了什么以及你想要发生什么。
    • @robmayoff 真的很抱歉。那是我的错误。它现在工作正常。实际上我也在使用另一个类别,方法很混乱并调用它们的方法。
    • 重复次数是关键。
    【解决方案2】:

    我已经 fork 由“Dreddik”制作的“uiimage-from-animated-gif”并添加了一个委托方法

    #pragma mark - AnimatedGifDelegate
    - (void)animationWillRepeat:(AnimatedGif *)animatedGif
    {
        NSLog(@"animationWillRepeat");
        //[animatedGif stop];
    }
    

    所以你知道动画会重复的时间,并在每次重复的中间做你自己的事情。您可以计算动画重复的次数,并在达到一定次数时停止动画。

    分叉的存储库位于https://github.com/rishi420/Animated-GIF-iPhone

    【讨论】:

      【解决方案3】:

      在开始/stp 动画和管理动画计数方面遇到问题 所以用this pod

      imageViewObject.animate(withGIFNamed: "gif_name", loopCount: 1) {
                  print("animating image")
          }
      

      开始/停止动画:

      imageViewObject.isAnimatingGIF ? imageViewObject.stopAnimatingGIF() : imageViewObject.startAnimatingGIF()    
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-02
        • 2021-01-25
        • 2011-04-22
        • 1970-01-01
        • 2016-09-19
        • 2022-11-05
        • 2012-02-17
        相关资源
        最近更新 更多