美文网首页
使用SDWebImage下载高分辨率图时,导致内存memory暴

使用SDWebImage下载高分辨率图时,导致内存memory暴

作者: 冰land | 来源:发表于2017-11-30 16:09 被阅读158次

最近在做公司项目中,发现用sd_setImageWithURL:加载图片的时候,内存会保证,通过instrument调试工具发现:


instrument分析图

代码定位


image
在解压缩的时候,出现内存飙升

解决方案
1、首先在封装的控制器中定义变量用于存储原设置:

static BOOL SDImageCacheOldShouldDecompressImages = YES;
static BOOL SDImagedownloderOldShouldDecompressImages = YES;

2、loadView中保存原设置并且禁用解压缩

- (void)loadView {
    [super loadView];
    SDImageCache *canche = [SDImageCache sharedImageCache];
    SDImageCacheOldShouldDecompressImages = canche.shouldDecompressImages;
    canche.shouldDecompressImages = NO;
    
    SDWebImageDownloader *downloder = [SDWebImageDownloader sharedDownloader];
    SDImagedownloderOldShouldDecompressImages = downloder.shouldDecompressImages;
    downloder.shouldDecompressImages = NO;
}

3、delloc中恢复原设置:

-(void)dealloc {
    SDImageCache *canche = [SDImageCache sharedImageCache];
    canche.shouldDecompressImages = SDImageCacheOldShouldDecompressImages;

    SDWebImageDownloader *downloder = [SDWebImageDownloader sharedDownloader];
    downloder.shouldDecompressImages = SDImagedownloderOldShouldDecompressImages;
}

再次用instrument跑了一下,方法果然有效,内存彻底降下来了,如图:


image

相关文章

网友评论

      本文标题:使用SDWebImage下载高分辨率图时,导致内存memory暴

      本文链接:https://www.haomeiwen.com/subject/crwvbxtx.html