SDWebImage

SDWebImage实现图片展示、缓存、清除缓存 

1.

/*

图片显示

*/

        [self.imageView sd_setImageWithURL:[NSURL URLWithString:urlString]];
        
        [self imageCachesWithUrl:[NSURL URLWithString:model.mediumLogo]];

2.
/*
图片缓存
*/
-(void)imageCachesWithUrl:(NSURL*)url{


    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    
    [manager downloadImageWithURL:url options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        NSLog(@"显示当前进度");
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        
        NSLog(@"下载完成");
    }];

}

3./*

清理图片缓存

*/

float tmpSize = [[SDImageCache sharedImageCache] getSize];
                
                [[SDImageCache sharedImageCache] clearDisk];
               // float tmpSize = [[SDImageCache sharedImageCache]checkTmpSize];
               // [[SDWebImageManager sharedManager].imageCache clearMemory];
                
               // float tmpSize= [SDWebImageManager sharedManager].imageCache.getSize;
                NSString *clearCacheName = tmpSize >= 1 ? [NSString stringWithFormat:@"清理缓存(%.2fM)",tmpSize/1000000] : [NSString stringWithFormat:@"清理缓存(%.2fK)",tmpSize/1000];
                NSLog(@"%@",clearCacheName);

获取图片

[self.imgBtn1.bgImgView sd_setImageWithURL:[NSURL URLWithString:arr[0]] placeholderImage:[UIImage imageNamed:@"img_holder_a"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                CGFloat w = CGImageGetWidth(image.CGImage);
            CGFloat h= CGImageGetHeight(image.CGImage);

            }];
           

1.加载动图

FLAnimatedImageView

SDWebImage加载多张图片,有时候,程序有时候会出现异常卡顿,严重的时候甚至会直接崩溃,查看原因,控制台返回时内存溢满。

SDWebimage会做三件事,一判断本地是否有这张图,二有的时候直接从本地取图片,三没有的时候去网络下载。
通过对SDWebImage代码的分析,发现他都会调用下面这个方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (nullable UIImage *)diskImageForKey:(nullable NSString *)key {
    NSData *data = [self diskImageDataBySearchingAllPathsForKey:key];
    if (data) {
        UIImage *image = [UIImage sd_imageWithData:data];
        image = [self scaledImageForKey:key image:image];
        if (self.config.shouldDecompressImages) {
            image = [UIImage decodedImageWithImage:image];
        }
        return image;
    }
    else {
        return nil;
    }
}

其中有这行代码:

1
UIImage *image = [UIImage sd_imageWithData:data];

图片取出来的时候就已经巨大无比,占用了很大的内存,导致内存来不及释放就崩溃。

点进这个sd_imageWithData方法内部,发现这里面对图片的处理是直接按照原大小进行的,如果图片过大将导致占用了大量内存。所以我们需要在这里对图片做一次等比的压缩。

1.在UIImage+MultiFormat这个类里面添加如下压缩方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
+(UIImage *)compressImageWith:(UIImage *)image
{
    float imageWidth = image.size.width;
    float imageHeight = image.size.height;
    float width = 640;
    float height = image.size.height/(image.size.width/width);
    
    float widthScale = imageWidth /width;
    float heightScale = imageHeight /height;
    
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    
    if (widthScale > heightScale) {
        [image drawInRect:CGRectMake(0, 0, imageWidth /heightScale , height)];
    }
    else {
        [image drawInRect:CGRectMake(0, 0, width , imageHeight /widthScale)];
    }
    
    // 从当前context中创建一个改变大小后的图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    
    return newImage;
    
}

然后在UIImage+MultiFormat文件中调用如下代码
如下图:

1
2
3
if (data.length/1024 > 128) {
            image = [self compressImageWith:image];
        }

2.在SDWebImageDownloaderOperation中将接下来的data数据用第一步中等比转换后的image来获取。

在didCompleteWithError代理方法中,如下图:

添加代码

1
2
NSData *data = UIImageJPEGRepresentation(image, 1);
                self.imageData = [NSMutableData dataWithData:data];

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值