SDWebImage

上文 码完YYWebImage后发现,SDWebImage 已经更新4.0-Beta2,一并记录一下。

动图

//加载 Gif 图
UIImage *image = [UIImage sd_animatedGIFWithData:data];
//判断图片格式
[NSData sd_imageFormatForImageData:data];

- (void)sd_setAnimationImagesWithURLs:(nonnull NSArray *)arrayOfURLs;
    
 - (void)sd_cancelCurrentAnimationImagesLoad;

图片加载

    NSURL *url = [NSURL URLWithString:@"http://www.fanghoo.cn/upload/147494351380663.jpg];
    [imageView sd_setImageWithURL:url
                 placeholderImage:nil
                          options:SDWebImageProgressiveDownload
                         progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
                             NSLog(@"%.2f",(float)receivedSize/expectedSize);
                         } completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                             // 先做内存缓存,在做磁盘缓存
                             switch (cacheType) {
                                 case SDImageCacheTypeNone:
                                     NSLog(@"直接从网络下载");
                                     break;
                                 case SDImageCacheTypeDisk:
                                     NSLog(@"从磁盘缓存");
                                     break;
                                 case SDImageCacheTypeMemory:
                                     NSLog(@"从内存缓存");
                                     break;
                                 default:
                                     break;
                             }

                         }];

 [[SDWebImageManager sharedManager] loadImageWithURL:url
                                             options:SDWebImageRetryFailed
                                            progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
                 NSLog(@"%.2f",(float)(receivedSize/expectedSize));
                                          } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
                  imageView.image = image;
}];

检测缓存照片

[[SDWebImageManager sharedManager] cachedImageExistsForURL:url
                                                completion:^(BOOL isInCache) {
        NSLog(@"是否有缓存%ld",(unsigned long)isInCache);
    }];
[[SDWebImageManager sharedManager ] diskImageExistsForURL:url
                                               completion:^(BOOL isInCache) {
        NSLog(@"沙盒是否有缓存%ld",(unsigned long)isInCache);
                                                       
                                                   }];

清理缓存

[[SDImageCache sharedImageCache] getSize];//缓存信息 
[[SDImageCache sharedImageCache] clearMemory]; 
[[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
[[SDWebImageManager sharedManager].imageCache clearDiskOnCompletion:nil];
[[SDWebImageManager sharedManager].imageCache clearMemory];

你可能感兴趣的:(SDWebImage)