最近在做公司项目中,发现用sd_setImageWithURL:加载图片的时候,内存会保证,通过instrument调试工具发现:
![](https://img.haomeiwen.com/i2464399/7fc6b9e30827d08e.png)
代码定位
![](https://img.haomeiwen.com/i2464399/b69d0031fe17d626.png)
在解压缩的时候,出现内存飙升
解决方案
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跑了一下,方法果然有效,内存彻底降下来了,如图:
![](https://img.haomeiwen.com/i2464399/7747658f9b6ca7fb.png)
网友评论