在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];