完美解决SDWebImage加载多个图片内存崩溃的问题
2016-09-03 15:42
429 查看
SDWebImage大家肯定都恨熟悉了,国内外太多的App使用其进行图片加载。
但是最近在使用过程中发现,我用SDWebImage加载多个图片,类似微博动态那种,在加载的过程中。我发现当图片分辨率比较大的时候(不是图片大),加载几张图片就崩溃了。
网上说可以每次加载图片清空memcache,但是效果并不好。
这地方采用的方法是:
第一步:在 UIImage+MultiFormat这个类里面添加如下压缩方法
+(UIImage *)compressImageWith:(UIImage *)image
{
float imageWidth = image.size.width;
float imageHeight = image.size.height;
float width =
320;
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 *)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];
if (data.length/1024 >
90) {
image = [self
compressImageWith:image];
}
UIImageOrientation orientation = [self
sd_imageOrientationFromImageData:data];
if (orientation !=
UIImageOrientationUp) {
image = [UIImage
imageWithCGImage:image.CGImage
scale:image.scale
orientation:orientation];
}
}
return image;
}
第三步:
就是在SDWebImageDownloaderOperation的connectionDidFinishLoading方法里面的:
UIImage *image = [UIImage
sd_imageWithData:self.imageData];
NSString
*key = [[SDWebImageManager
sharedManager]
cacheKeyForURL:self.request.URL];
image = [self
scaledImageForKey:key
image:image];
NSData *data =
UIImageJPEGRepresentation(image,
1);
self.imageData = [NSMutableData
dataWithData:data];
最后;再配合
[[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];(图片加载后使用)大功告成,亲测内存基本变化不大,自动释放也来得及。
但是最近在使用过程中发现,我用SDWebImage加载多个图片,类似微博动态那种,在加载的过程中。我发现当图片分辨率比较大的时候(不是图片大),加载几张图片就崩溃了。
网上说可以每次加载图片清空memcache,但是效果并不好。
[[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];
这地方采用的方法是:
第一步:在 UIImage+MultiFormat这个类里面添加如下压缩方法
+(UIImage *)compressImageWith:(UIImage *)image
{
float imageWidth = image.size.width;
float imageHeight = image.size.height;
float width =
320;
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 *)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];
if (data.length/1024 >
90) {
image = [self
compressImageWith:image];
}
UIImageOrientation orientation = [self
sd_imageOrientationFromImageData:data];
if (orientation !=
UIImageOrientationUp) {
image = [UIImage
imageWithCGImage:image.CGImage
scale:image.scale
orientation:orientation];
}
}
return image;
}
第三步:
就是在SDWebImageDownloaderOperation的connectionDidFinishLoading方法里面的:
UIImage *image = [UIImage
sd_imageWithData:self.imageData];
NSString
*key = [[SDWebImageManager
sharedManager]
cacheKeyForURL:self.request.URL];
image = [self
scaledImageForKey:key
image:image];
NSData *data =
UIImageJPEGRepresentation(image,
1);
self.imageData = [NSMutableData
dataWithData:data];
最后;再配合
[[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];(图片加载后使用)大功告成,亲测内存基本变化不大,自动释放也来得及。
相关文章推荐
- iOS开发 - 完美解决SDWebImage加载多个图片内存崩溃的问题
- 完美解决SDWebImage加载多个图片内存崩溃的问题
- 完美解决SDWebImage加载多个图片内存崩溃的问题
- 解决SDWebImage加载多个图片内存崩溃的问题
- 解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题解决方案参考
- 解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题
- 解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题
- 解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题
- 解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题(刚刚)
- 解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题
- 关于SDWebImage加载高清图片导致app崩溃的问题
- 解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题
- iOS-解决SDWebImage加载多张图片内存溢出
- 解决使用SDWebImage加载图片,图片为空的问题
- iOS 使用SDWebImage加载gif链接图片时内存过大问题
- 解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题
- 解决MWPhotoBrowser中的SDWebImage加载大图导致的内存警告问题
- 1.用SDWebImage为UIImageView加载图片时遇到的问题
- iOS-OC-tableview SDWebImage加载多张图片内存警告(received memory warning)
- SDWebImage的实现机制以及解决tableView卡的问题和实现图片的缓存机制