首先需要先下載FLAnimatedImage支持gif圖片的播放砌们,將該框架導(dǎo)入項目中眯停,然后在SDCollectionViewCell.h文件中導(dǎo)入#import "FLAnimatedImageView.h"媳禁,將cell中的imageview屬性的類替換成FLAnimatedImageView戒洼,再在SDCollectionViewCell.m文件中的setupImageView方法中初始化imageView改成FLAnimatedImageView* imageView = [[FLAnimatedImageView alloc] init];
接下來在SDCycleScrollView.m文件中導(dǎo)入
#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圖保存到本地的
方法的實現(xiàn):
+(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); ? ? ? ? ? ? //圖片下載已完成,處理數(shù)據(jù) ? ? ? ? } ?? ? ? ? ? ? ? ? if (block) { ? ? ? ? ? ? block(location.path,error); ? ? ? ? } ?? ? ? ? ? ? }]; ? ? [task resume]; ?? ? }
這樣就大功告成了!沸版!