SDWebImage库自带的gif图加载方法
UIImageView *sdimageView = [[UIImageView alloc]init];
[sdimageView sd_internalSetImageWithURL:[NSURL URLWithString:@"https://i.pinimg.com/originals/07/44/38/074438e7c75034df2dcf37ba1057803e.gif"] placeholderImage:nil options:0 context:nil setImageBlock:^(UIImage * _Nullable image, NSData * _Nullable imageData, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
[sdimageView setImage:[UIImage sd_imageWithGIFData:imageData]];
} progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
}];
sdimageView.frame = CGRectMake(0, 0, 400, 300);
sdimageView.center = self.view.center;
[self.view addSubview:sdimageView];
动画很流畅,满帧运行,但是内存占用是真的大,如下图所示
FLanimatedImage 库
FLAnimatedImageView *sdimageView = [[FLAnimatedImageView alloc]init];
[sdimageView sd_internalSetImageWithURL:[NSURL URLWithString:@"https://i.pinimg.com/originals/07/44/38/074438e7c75034df2dcf37ba1057803e.gif"] placeholderImage:nil options:0 context:nil setImageBlock:^(UIImage * _Nullable image, NSData * _Nullable imageData, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
// [sdimageView setImage:[UIImage sd_imageWithGIFData:imageData]];
sdimageView.animatedImage = [[FLAnimatedImage alloc]initWithAnimatedGIFData:imageData];
} progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
}];
sdimageView.frame = CGRectMake(0, 0, 400, 300);
sdimageView.center = self.view.center;
[self.view addSubview:sdimageView];
掉帧严重,播放速度明显变慢了很多,但是内存和CPU占用率比较合理
AsyncDisplayKit(Texture库)
ASNetworkImageNode *imageNode = [[ASNetworkImageNode alloc] init];
imageNode.URL = [NSURL URLWithString:@"https://i.pinimg.com/originals/07/44/38/074438e7c75034df2dcf37ba1057803e.gif"];
// Uncomment to see animated webp support
// imageNode.URL = [NSURL URLWithString:@"https://storage.googleapis.com/downloads.webmproject.org/webp/images/dancing_banana2.lossless.webp"];
imageNode.frame = self.view.bounds;
imageNode.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
imageNode.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubnode:imageNode];
轻微掉帧,播放比较流畅,内存占用如下
总结
对于大部分网络Gif图来说,不建议使用SDwebimage自带的加载方式,因为指不定配置的图片帧数过多或者分辨率大一点,线上用户内存就爆了
- 如果gif图没有那么大,帧数不高,可以使用FlanimationImage这个轻量级框架
- 超大GIF和高帧率,可以考虑引入Texture 库,这个库比较庞大,GIF图只是其冰山一角