YYAnimatedImageView--gif在ios14之后只能播放一次

在iOS14之后,使用imageIo 内的CGImageDestinationAddImage合成的gif在YYAnimatedImageView上只播放了一次就停止了,没有无限循环播放。

在网上查了很多,gif的播放次数与内部kCGImagePropertyGIFLoopCount这个参数有关。我自己生成的gif,转成NSData后,去获取loopcount不为0,这里的gifProperty有时候为nil最后也打印为0,因为gifProperty字典返回的是对象而不是原始类型。返回的对象是NSNumber,其默认值转换为int 0。我没有检查nil。该属性不循环时不存在

-(void)prepareGif:(NSData*)data {
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef) data, NULL);
    CFDictionaryRef properties = CGImageSourceCopyProperties(source, nil);
    NSDictionary* imageProperties = (__bridge NSDictionary*) properties;
    NSDictionary* gifProperty = imageProperties[(NSDictionary*) kCGImagePropertyGIFDictionary];
    int loopCount = [gifProperty[(NSString*) kCGImagePropertyGIFLoopCount] intValue]; // returns 0 for LoopCount 1 and LoopCount 0 and returns 1 for LoopCount 2
    NSLog(@" --- %d",loopCount);
}

YYImage作者已经停更了好很多年了,所以我们使用FLAnimatedImage,在FLAnimatedImage.m中加入image.loopCount = 0来解决。

- (instancetype)initWithAnimatedGIFData:(NSData *)data
{
    FLAnimatedImage *image = [self initWithAnimatedGIFData:data optimalFrameCacheSize:0 predrawingEnabled:YES];
    image.loopCount = 0;
    return image;
}

_imageView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:data];

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值