加载图片
[self.imvIcon sd_setImageWithURL:[model.filerawname mj_stringEncodeUrl] placeholderImage:kDefaultImage];
SDWebImageManager 的使用
###SDWebImageManager将图片下载和图片缓存组合起来了
SDWebImageManager *manager = [SDWebImageManager sharedManager];
NSURL *imgUrl = [NSURL URLWithString:urlPath];
[manager loadImageWithURL:imgUrl options:0 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) {
if (image && finished) {
self.myImageView.image = image;
}
}];
SDWebImageDownloader 不缓存
SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
NSURL *imgUrl = [NSURL URLWithString:urlPath];
// 利用 SDWebImage 框架提供的功能下载图片,不会缓存
[downloader downloadImageWithURL:imgUrl options:SDWebImageDownloaderUseNSURLCache progress:^(NSInteger receivedSize, NSInteger expectedSize,NSURL * _Nullable targetURL) {
} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if(finished)
{
[[SDImageCache sharedImageCache] storeImage:image forKey:urlPath toDisk:YES completion:nil];
self.myImageView.image = image;
}
}];
单独使用 SDImageCache 异步缓存图片
[[SDImageCache sharedImageCache] storeImage:image forKey:urlPath toDisk:YES completion:nil];
加载回调
[_imageVIedw sd_setImageWithURL:imgUrl2 placeholderImage:[UIImage imageNamed:@"logo_del_pro"] options:SDWebImageLowPriority | SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//判断图片是否有值,避免出现除以零的情况导致崩溃
if (image)
{
NSData * imageData = UIImageJPEGRepresentation(image,1);
NSInteger length = [imageData length]/1024;
NSLog(@"---%ld",length);
}
}];
判断是否已缓存图片
UIImage *productImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:urlPath];
if(productImage)
{
NSLog(@"存在图片");
self.imageVIedw.image = productImage;
}
else
{
NSLog(@"不存在图片");
}
清除缓存大小
NSInteger size = [[SDImageCache sharedImageCache] getSize];//获取缓存大小
NSString *sizeString = [NSString fileSizeWithInterge:size];//转换为KB
[[SDImageCache sharedImageCache] clearDisk];//清除点击
加载GIF
###
#import "FLAnimatedImageView.h"
#import "FLAnimatedImage.h"
- (FLAnimatedImageView *)gifImageView
{
if (!_gifImageView) {
_gifImageView = [[FLAnimatedImageView alloc] init];
_gifImageView.contentMode = UIViewContentModeScaleAspectFit;
NSString *name = @"reconizeImage.gif";
NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
_gifImageView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:imageData];
}
return _gifImageView;
}
### 4.0 之后才能用
- (UIImageView *)gifImageView1
{
if (!_gifImageView1) {
_gifImageView1 = [[UIImageView alloc] init];
_gifImageView1.contentMode = UIViewContentModeScaleAspectFit;
NSString *name = @"reconizeImage.gif";
NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
_gifImageView1.image = [UIImage sd_animatedGIFWithData:imageData];
}
return _gifImageView1;
}
SDWebImage 整理
https://www.jianshu.com/p/06f0265c22eb