SDWebImage加载高分辨率图片内存暴涨问题
2021-11-11 本文已影响0人
骑着蜗牛追流星
目前使用的SDWebImage版本为4.3.3 ,其他版本自测~
SDWebImageImageIOCoder.m
// 添加方法
-(UIImage *)compressImageWith:(NSData *)imageDD
{
UIImage *image = [UIImage imageWithData:imageDD];
float imageWidth = image.size.width;
float imageHeight = image.size.height;
//这里的width看自己需求,如果嫌图片模糊就加大点,我这里屏幕宽度*2.5刚好达到需求
float width = [UIScreen mainScreen].bounds.size.width * 2.5;
if (imageWidth < width) {
width = imageWidth;
}
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 *)decodedImageWithData:(NSData *)data 方法
- (UIImage *)decodedImageWithData:(NSData *)data {
if (!data) {
return nil;
}
//UIImage *image = [[UIImage alloc] initWithData:data];
UIImage *image = [self compressImageWith:data];
#if SD_MAC
return image;
#else
if (!image) {
return nil;
}
UIImageOrientation orientation = [[self class] sd_imageOrientationFromImageData:data];
if (orientation != UIImageOrientationUp) {
image = [[UIImage alloc] initWithCGImage:image.CGImage scale:image.scale orientation:orientation];
}
return image;
#endif
}