iOS FLAnimatedImage控制Gif播放次数

新手入门
请多指教

前言

要展示gif图片啊,但是UI提供的gif图是循环的,这边需求gif只在打开的时候播放一次就行。

分析

FLAnimatedImage直接读取了gif本身的循环次数,并赋值给了loopCount字段。本意想着,这多简单,直接改下loopCount就完事了,结果发现这个字段是readonly。。。只允许在初始化的时候赋值,通过查看源码,该值是直接从gif图片数据中解析而来,没有其它的干扰其值的方式。

//
//  FLAnimatedImage.h
//  Flipboard
//  源码

@property (nonatomic, assign, readonly) NSUInteger loopCount; // 0 means repeating the animation indefinitely

================================

//
//  FLAnimatedImage.m
//  Flipboard
//  源码

        // Get `LoopCount`
        // Note: 0 means repeating the animation indefinitely.
        // Image properties example:
        // {
        //     FileSize = 314446;
        //     "{GIF}" = {
        //         HasGlobalColorMap = 1;
        //         LoopCount = 0;
        //     };
        // }
        NSDictionary *imageProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(_imageSource, NULL);
        _loopCount = [[[imageProperties objectForKey:(id)kCGImagePropertyGIFDictionary] objectForKey:(id)kCGImagePropertyGIFLoopCount] unsignedIntegerValue];


网上确实不少人建议直接把readonly改成可读写。这样自己想怎么循环就怎么循环了。

但是,这样需要更改的是FLAnimatedImage.h文件,对以后的FLAnimatedImage升级和维护都可能存在隐患。

解决方式

Note: 0 means repeating the animation indefinitely.
注: 0 意味着循环播放,其它值则为播放次数。

1. 暴力解决(不建议)

直接改loopCount的属性,去掉只读的属性。
然后自己想怎么赋值就怎么赋值了。
不过刚才也说过了,存在升级隐患,非常不建议,但也不失为一种方式。

//
//  FLAnimatedImage.h
//  Flipboard
//  源码

@property (nonatomic, assign) NSUInteger loopCount; // 0 means repeating the animation indefinitely


===========================
// 实际使用时
FLAnimatedImage *gifImage = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"gifTest" withExtension:@"gif"]]];
gifImage.loopCount = 1;

FLAnimatedImageView *gifView = [[FLAnimatedImageView alloc] init];
gifView.animatedImage = gifImage;
[self.view addSubview:gifView];

2. 换个Gif 图(灰常推荐)

哈哈,让UI重新出一套需求要求的Gif图,简单省事。

3. 使用setLoopCompletionBlock监控

该block为Gif动画每执行完一次的回调。
每次执行完之后,可以根据loopCount或者自行计数来觉得继续执行亦或者停止执行。
下方为将循环播放的gif执行完一次之后关闭执行的操作,代码仅供参考。

    [gifView setLoopCompletionBlock:^(NSUInteger loopCountRemaining) {
        // 判断是循环播放,并且当前仍在播放,则停止gif
        if (weakSelf.gifView.animatedImage.loopCount == 0 && weakSelf.gifView.isAnimating) {
            [weakSelf.gifView stopAnimating];
        }
    }];

将不循环的gif,设置为循环。
这里因为各种相关值都是readonly状态,在不改源码的情况下很难修改对应的值,所以考虑重置所有的值,将animatedImage的值重新赋值给FLAnimatedImageView.令其重新播放gif。目前尚不确认内存会出现频繁抖动。希望参考的小伙伴慎重考虑。

    [_gifView setLoopCompletionBlock:^(NSUInteger loopCountRemaining) {
        // loopCount !=0 为不循环,
        if (weakSelf.gifView.animatedImage.loopCount != 0) {
            // stop 当前 animatedImage
            [weakSelf.gifView stopAnimating];

            // 取出animatdImage重新赋值给imageView以重置readonly的loopCount等值
            FLAnimatedImage *image = weakSelf.gifView.animatedImage;
            weakSelf.gifView.animatedImage = nil;

            weakSelf.gifView.animatedImage = image;
        }
    }];

4. 考虑使用动画

想了一想,如果不是必须要求使用Gif的情况,可以考虑从Gif中抽取帧,做成原生的帧动画,或者使用其它属性动画完成一样的效果,这个要结合实际需求来做。

参考文献

FLAnimatedImage (https://github.com/Flipboard/FLAnimatedImage);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值