SDWebImage-使用

加载图片


[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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这个类库提供一个UIImageView类别以支持加载来自网络的远程图片。具有缓存管理、异步下载、同一个URL下载次...
    5c7a7cf606fc阅读 3,381评论 0 6
  • 这个类库提供一个UIImageView类别以支持加载来自网络的远程图片。具有缓存管理、异步下载、同一个URL下载次...
    devning阅读 2,263评论 0 0
  • SDWebImage是当下最流行的一个三方图片处理框架,我们使用较多的是它提供的UIImageView分类,支持从...
    伶俐ll阅读 605评论 0 2
  • SDWebImage的使用 SDWebImage的所有的下载策略 SDWebImage实现逻辑分析 总结一下: 主...
    夏天的风_song阅读 2,499评论 0 2
  • Web image(网络图像) 该库提供了一个支持来自Web的远程图像的UIImageView类别它提供了: 添加...
    俊瑶先森阅读 19,609评论 2 9