关于GIF图片处理几种方式

/**

*  设置gif图片

*

*  @param gifImage gif

*/

-(void)setGifImage:(NSString *)gifImage{

//    gifImage = @"http://apkcdn.mquba.com/wysy/tuji/img_pic/20160405gif1.gif";

//    gifImage = @"http://apkcdn.mquba.com/wysy/tuji/img_pic/20160405gif3.gif";

//    gifImage = @"http://apkcdn.mquba.com/wysy/tuji/img_pic/20160405gif2.gif";

[self DownloadGIFWithProgressUsingFLAnimatedView:gifImage];

}

/**

*  采用SDWebImage处理gif图片,这种方法有点BUG,进度在网速不好情况下会卡住,停止下载...不适合处理gif图片

*

*  @param gifImage gif地址

*/

- (void)setGifWithProgressUsingSDWebImage:(NSString *)gifImage{

@weakify(self)

[_thumbImageView sd_setImageWithURL:[NSURL URLWithString:gifImage] placeholderImage:nil options:SDWebImageProgressiveDownload|SDWebImageCacheMemoryOnly progress:^(NSInteger receivedSize, NSInteger expectedSize) {

@strongify(self)

_progressView.hidden = NO;

[self setCurrentProgress:(receivedSize * 1.0 / expectedSize)];

} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

_progressView.hidden = YES;

}];

}

/**

*  下载带有进度的gif,用FLAnimatedView处理gif动画

*

*  @param gifImage gif

*/

- (void)DownloadGIFWithProgressUsingFLAnimatedView:(NSString *)gifImage{

__block NSString * diskPath;

NSData *animatedImageData;

NSString *const filename = gifImage.lastPathComponent;

NSString *const filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

diskPath = [filePath stringByAppendingPathComponent:filename];

animatedImageData = [[NSFileManager defaultManager] contentsAtPath:diskPath];

if (animatedImageData) {

_thumbImageView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:animatedImageData];

return ;

}

@weakify(self)

[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:gifImage] options:SDWebImageDownloaderUseNSURLCache progress:^(NSInteger receivedSize, NSInteger expectedSize) {

@strongify(self)

_progressView.hidden = NO;

[self setCurrentProgress:(receivedSize * 1.0 / expectedSize)];

} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {

_progressView.hidden = YES;

[data writeToFile:diskPath atomically:YES];

_thumbImageView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:data];

}];

}

/**

*  下载gif图片,不带下载进度的

*

*  @param gifImage gif图片路径

*/

- (void)dataTaskWithRemoteGifImage:(NSString *)gifImage{

__block NSString * diskPath;

NSString *const filename = gifImage.lastPathComponent;

NSString *const filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

diskPath = [filePath stringByAppendingPathComponent:filename];

NSData *animatedImageData = [[NSFileManager defaultManager] contentsAtPath:diskPath];

if (animatedImageData) {

_thumbImageView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:animatedImageData];

return;

}

[[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:gifImage] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

if (data) {

[data writeToFile:diskPath atomically:YES];

dispatch_async(dispatch_get_main_queue(), ^{

_thumbImageView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:data];

});

}

}] resume] ;

[self setNeedsDisplay];

}

/**

*  显示当前进度,在子线程绘图

*

*  @param pro 进度百分比

*/

- (void)setCurrentProgress:(float)persentage{

[_progressView setProgress:persentage Animated:NO];

}

/**

*  从本地下载gif

*

*  @param imageName gif名字

*/

- (void)loadGifFromBundle:(NSString*)imageName{

NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:imageName ofType:nil];

dispatch_async(dispatch_get_global_queue(0, 0), ^{

NSData  *imageData = [NSData dataWithContentsOfFile:filePath];

dispatch_async(dispatch_get_main_queue(), ^{

//              [_thumbImageView setImage:[UIImage sd_animatedGIFWithData:imageData]];采用SDWebImage的gif,当gif加载较多时候、或者屏幕滚动较快时候会卡顿,所以换为FLAnimatedImage

_thumbImageView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:imageData];

});

});

}

/**

*  设置普通图片

*/

- (void)setImageURL:(NSURL *)imageURL{

_progressView.hidden = YES;

//    [self setGifImage:[NSString stringWithFormat:@"%@",imageURL]];

[_thumbImageView sd_setImageWithURL:imageURL placeholderImage:nil];

}

你可能感兴趣的:(关于GIF图片处理几种方式)