問題:控制器里下載N張圖片,當圖片過大(圖片質量和尺寸)使用SDWebImage時,快速滑動頁面進行加載會撐爆內存。很多博客都說使用如下方式,在合適的時機清除緩存來處理,然并卵。
[[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];
處理方式
在UIImage+MultiFormat
中修改方法+ (UIImage *)sd_imageWithData:(NSData *)data
,根據圖片的data得到圖片的大小,判斷大小超過一定閾值時對圖片進行壓縮——通過上下文重新繪制bitmap。
+(UIImage *)compressImageWith:(UIImage *)image {
float imageWidth = image.size.width;
float imageHeight = image.size.height;
float width = 640;
float height = (image.size.height * width) / image.size.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;
}
同時,在AppDelegate
收到內存警告時處理:
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[[SDWebImageManager sharedManager] cancelAll];
[[SDWebImageManager sharedManager].imageCache clearMemory];
}
其它處理方式
如果不影響SD原有代碼,可直接通過一個類別,對原有的sd_imageOrientationFromImageData方法和自定義的方法進行切換處理。
@implementation UIImage (SDImageCompress)
/**
initialize方法會在所有的類加載完畢后 第一次向類發送消息的時候 會被調用
這里并不能用load因為你不能確定哪個分類先加載完畢了,initialize的調用確保該類及其分類均已加載完畢,所以下面是可以拿到方法的指針。
*/
+ (void)initialize {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
// 替換SD里面sd_imageWithData的方法為自己的方法
Method originalMethod = class_getClassMethod(class, @selector(sd_imageWithData:));
Method swizzledMethod = class_getClassMethod(class, @selector(customSd_imageWithData:));
method_exchangeImplementations(originalMethod, swizzledMethod);
// 因為sd_imageWithData里面回調sd里面自己的方法,然后通過自己寫一個方法,再將該方法轉回到sd里面
Method originalMethod1 = class_getClassMethod(class, @selector(customSd_imageOrientationFromImageData:));
Method swizzledMethod1 = class_getClassMethod(class, @selector(sd_imageOrientationFromImageData:));
method_exchangeImplementations(originalMethod1, swizzledMethod1);
#pragma clang diagnostic pop
});
}
/*
sd內部處理下載完畢數據的方法
* This file is part of the SDWebImage package.
* (c) Olivier Poitrey <rs@dailymotion.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
+ (nullable UIImage *)customSd_imageWithData:(nullable NSData *)data {
if (!data) {
return nil;
}
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 > 128 * 1024) { // 大于128KB時進行壓縮處理
image = [self compressImageWith:image];
}
UIImageOrientation orientation = [self customSd_imageOrientationFromImageData:data];
if (orientation != UIImageOrientationUp) {
image = [UIImage imageWithCGImage:image.CGImage
scale:image.scale
orientation:orientation];
}
}
return image;
}
@end