您的位置:首页 > 其它

SDWebImage加载大图导致的内存警告问题

2015-12-25 21:05 351 查看
MWPhotoBrowser是一个非常不错的照片浏览器,在github的star接近3000个,地址:https://github.com/mwaterfall/MWPhotoBrowser.git

MWPhotoBrowser来加载小图1M以下的都应该不会有内存警告的问题。如果遇到大图,3M、4M、5M的大图,很有可能导致内存警告。最近我就遇到这个问题,很是头疼。来回滑动查看照片内存飙到100M以上:



网上查了很多资料,都没有解决问题。

我们来看一下MWPhotoBrowser,其实MWPhotoBrowser用的是SDWebImage来下载图片的。地址:https://github.com/rs/SDWebImage.git

在github看到SDWebImage的介绍,后面说到:

Future Enhancements

LRU memory cache cleanup instead of reset on memory warning

看到这个真是欲哭无泪啊。

再去看看SDWebImage的,有个人提问了:

How to disable "memory cache"? I don't want memory cache, it used a lot of memory and got memory waring easily, disk is enough for me...

有人回答:

There is no way to disable the memory cache. But the cache is designed to flush itself when you get a memory warning, so you shouldn't need to worry it.

说的是SDWebImage遇到内存警告会自动释放内存,但是这还是解决不了问题,加载大图的时候,内存会突然蹦到100多M,在4s及以下的手机上跑,再就挂了。

还是没有解决内存警告的问题。怎么办呢?

我是这么解决的:

SDWebImage有一个SDWebImageDownloaderOperation类来执行下载操作的。里面有个下载完成的方法:

- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {

SDWebImageDownloaderCompletedBlock completionBlock = self.completedBlock;

@synchronized(self) {

CFRunLoopStop(CFRunLoopGetCurrent());

self.thread = nil;

self.connection = nil;

[[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];

}

if (![[NSURLCache sharedURLCache] cachedResponseForRequest:_request]) {

responseFromCached = NO;

}

if (completionBlock)

{

if (self.options & SDWebImageDownloaderIgnoreCachedResponse && responseFromCached) {

completionBlock(nil, nil, nil, YES);

}

else {

UIImage *image = [UIImage sd_imageWithData:self.imageData];

NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];

image = [self scaledImageForKey:key image:image];

// Do not force decoding animated GIFs

if (!image.images) {

image = [UIImage decodedImageWithImage:image];

}

if (CGSizeEqualToSize(image.size, CGSizeZero)) {

completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}], YES);

}

else {

completionBlock(image, self.imageData, nil, YES);

}

}

}

self.completionBlock = nil;

[self done];

}

其中,UIImage *image = [UIImage sd_imageWithData:self.imageData];就是将data转换成image。

再看看sd_imageWithData:这个方法:

+ (UIImage *)sd_imageWithData:(NSData *)data {

UIImage *image;

NSString *imageContentType = [NSData sd_contentTypeForImageData:data];

if ([imageContentType isEqualToString:@"image/gif"]) {

image = [UIImage sd_animatedGIFWithData:data];

}

#ifdef SD_WEBP

else if ([imageContentType isEqualToString:@"image/webp"])

{

image = [UIImage sd_imageWithWebPData:data];

}

#endif

else {

image = [[UIImage alloc] initWithData:data];

UIImageOrientation orientation = [self sd_imageOrientationFromImageData:data];

if (orientation != UIImageOrientationUp) {

image = [UIImage imageWithCGImage:image.CGImage

scale:image.scale

orientation:orientation];

}

}

return image;

}

这个方法在UIImage+MultiFormat里面,是UIImage的一个类别处理。这句话很重要image = [[UIImage alloc] initWithData:data]; SDWebImage把下载下来的data直接转成image,然后没做等比缩放直接存起来使用。所以,我们只需要在这边做处理即可:

UIImage+MultiFormat添加一个方法:

+(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;

}

然后在:image = [[UIImage alloc] initWithData:data];下面调用以下:

if (data.length/1024 > 1024) {

image = [self compressImageWith:image];

}

当data大于1M的时候做压缩处理。革命尚未成功,还需要一步处理。在SDWebImageDownloaderOperation的connectionDidFinishLoading方法里面的:

UIImage *image = [UIImage sd_imageWithData:self.imageData];

//将等比压缩过的image在赋在转成data赋给self.imageData

NSData *data = UIImageJPEGRepresentation(image, 1);

self.imageData = [NSMutableData dataWithData:data];

SDWebImage的知名度就不用说了,github上近10k的star,国内外太多的App使用其进行图片加载。

但是最近在使用过程中发现,在UITableView中不断加载更多的内容,使用SDWebImage会造成内存占用越来越大,导致memory warning最终terminate,稍微找了下问题原因,发现不少开发者都遇到过这个问题,中文的资料没有搜到该问题的解决办法,为了方便国内其他开发者遇到类似问题不浪费时间,这篇blog把解决方法记录如下:

首先检查了SDWebImage代码中对于memory warning的处理:

- (void)clearMemory {
[self.memCache removeAllObjects];
}


其中self.memCache是NSCache类型的,可以看到SDWebImage本身对内存警告执行了操作,但是并没有什么X用。

用Instruments的allocations分析了一下内存使用情况:


可以看到内存基本都在decodedImageWithImage:这个方法里被占用了,查看了这个方法的内部实现,感觉还是比较正常的,只好求助google

最后在github上找到了这条issue:https://github.com/rs/SDWebImage/issues/538

下面回复很多(看来这个问题很多人都遇到了),甚至SDWebImage的作者本人rs也在下面进行了很多的回复(虽然他的回复并没有什么X用),回复里说到的问题原因和解决办法归纳如下:

1.rs本人回复的:SDWebImage用到的NSCache会在合适的时候(memory warning)释放内存,很多应用在加载大量图片的时候没有出现这种情况;

2.这个问题是因为SDWebImage对GIF的支持的代码造成的,去掉相关代码即可;

3.decodedImageWithImage的实现直接retrun image即可;

4.https://twitter.com/0xced/status/332252283758845953,这条Twitter也是蛮幽默的,通俗的翻译就是这条Twitter中描述的解决办法拯救了那些因为SDWebImage导致问题的程序员于水火之中。

挨个试了试上面4中解决方法:

第一种没什么可说的了,之前分析源码的时候就已经看到,也就是说rs的解决办法没用。

第二种办法,阅读源码可以发现SDWebImage中对GIF的处理只会针对GIF图片,并不会影响到png或者其他格式的图片,因此这个办法我觉得没用,没有尝试。

第三种,直接导致内存占用原因由CG raster data变成了ImageIO_PNG_Data,也是没用。

第四种,最开始我在receive memory warning的时候调用了这段代码

[[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];


发现依然会出现问题,遂尝试了每次加载更多内容的时候都执行一次,终于内存不再持续增加了,也就是说第四种是最终的解决办法,在后续blog我可能会专门分析一下这个问题出现的原因和解决的原理。

总结如下:

在使用SDWebImage加载较多图片造成内存警告时,定期调用

[[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];


可解决。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: