首先需要先下载FLAnimatedImage支持gif图片的播放,将该框架导入项目中,然后在SDCollectionViewCell.h文件中导入#import "FLAnimatedImageView.h",将cell中的imageview属性的类替换成FLAnimatedImageView,再在SDCollectionViewCell.m文件中的setupImageView方法中初始化imageView改成FLAnimatedImageView* imageView = [[FLAnimatedImageView alloc] init];
接下来在SDCycleScrollView.m文件中导入
#import "UIImage+GIF.h"
#import "FLAnimatedImage.h"
第三步在collectionview的DataSource的代理方法- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath中添加该代码,如图
其中:
[Util getWebGifImageWithKey:key response:^(NSString *path, NSError *error) { }];
这个是我定义的工具类方法,用来下载gif图保存到本地的
方法的实现:
+(void)getWebGifImageWithKey:(NSString*)key response:(responseBlock)block{ NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:config]; NSURL *url = [NSURL URLWithString:key]; NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (error) { MLLog(@"gif download error:%@", error); } else { MLLog(@"gif download success, file path:%@",location.path); //图片下载已完成,处理数据 } if (block) { block(location.path,error); } }]; [task resume]; }
这样就大功告成了!!
网友评论