SDWebImage加載多張圖片卡頓閃退

網上有很多關于卡頓的解決辦法,這里就不在一一列舉,有很多方法我都試過,不過并不完全有用,因為我們后臺給的圖片是5000*5000這種超高分辨率的圖片,所以在轉data的時候造成內存消耗非常大,那么解決辦法就很清楚了。搜索sd_imageWithData:方法,在image創建之后對image做處理:

+ (UIImage *)sd_imageWithData:(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/1024 > 128) {

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;

}

將上面的代碼替換掉原先方法即可。

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

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容