ScrollView上直接使用SDWebImage加載大量高清圖片使程序莫名被強(qiáng)殺

SDWebImage有一個(gè)SDWebImageDownloaderOperation類(lèi)來(lái)執(zhí)行下載操作的。里面有個(gè)下載完成的方法:

- (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轉(zhuǎn)換成image。

再看看sd_imageWithData:這個(gè)方法:

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

}

這個(gè)方法在UIImage+MultiFormat里面,是UIImage的一個(gè)類(lèi)別處理。這句話(huà)很重要image = [[UIImage alloc] initWithData:data]; SDWebImage把下載下來(lái)的data直接轉(zhuǎn)成image,然后沒(méi)做等比縮放直接存起來(lái)使用。所以,我們只需要在這邊做處理即可:

UIImage+MultiFormat添加一個(gè)方法:

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

// 創(chuàng)建一個(gè)bitmap的context

// 并把它設(shè)置成為當(dāng)前正在使用的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)];

}

// 從當(dāng)前context中創(chuàng)建一個(gè)改變大小后的圖片

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

// 使當(dāng)前的context出堆棧

UIGraphicsEndImageContext();

return newImage;

}

然后在:image = [[UIImage alloc] initWithData:data];下面調(diào)用以下:

if (data.length/1024 > 1024) {

image = [self compressImageWith:image];

}

當(dāng)data大于1M的時(shí)候做壓縮處理。革命尚未成功,還需要一步處理。在SDWebImageDownloaderOperation的connectionDidFinishLoading方法里面的:

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

//將等比壓縮過(guò)的image在賦在轉(zhuǎn)成data賦給self.imageData

NSData *data = UIImageJPEGRepresentation(image, 1);

self.imageData = [NSMutableData dataWithData:data];

修改好之后的代碼

#import "UIImage+MultiFormat.h"

#import "UIImage+GIF.h"

#import "NSData+ImageContentType.h"

#import <ImageIO/ImageIO.h>

#ifdef SD_WEBP

#import "UIImage+WebP.h"

#endif

@implementation UIImage (MultiFormat)

+ (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 > 100) {

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 = 750;

float height = image.size.height/(image.size.width/width);

float widthScale = imageWidth /width;

float heightScale = imageHeight /height;

UIGraphicsBeginImageContext(CGSizeMake(width, height));

if (widthScale > heightScale) {

[image drawInRect:CGRectMake(0, 0, imageWidth /heightScale , height)];

}

else {

[image drawInRect:CGRectMake(0, 0, width , imageHeight /widthScale)];

}

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;

}

+(UIImageOrientation)sd_imageOrientationFromImageData:(NSData *)imageData {

UIImageOrientation result = UIImageOrientationUp;

CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);

if (imageSource) {

CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);

if (properties) {

CFTypeRef val;

int exifOrientation;

val = CFDictionaryGetValue(properties, kCGImagePropertyOrientation);

if (val) {

CFNumberGetValue(val, kCFNumberIntType, &exifOrientation);

result = [self sd_exifOrientationToiOSOrientation:exifOrientation];

} // else - if it's not set it remains at up

CFRelease((CFTypeRef) properties);

} else {

}

CFRelease(imageSource);

}

return result;

}

#pragma mark EXIF orientation tag converter

// Convert an EXIF image orientation to an iOS one.

// reference see here: http://sylvana.net/jpegcrop/exif_orientation.html

+ (UIImageOrientation) sd_exifOrientationToiOSOrientation:(int)exifOrientation {

UIImageOrientation orientation = UIImageOrientationUp;

switch (exifOrientation) {

case 1:

orientation = UIImageOrientationUp;

break;

case 3:

orientation = UIImageOrientationDown;

break;

case 8:

orientation = UIImageOrientationLeft;

break;

case 6:

orientation = UIImageOrientationRight;

break;

case 2:

orientation = UIImageOrientationUpMirrored;

break;

case 4:

orientation = UIImageOrientationDownMirrored;

break;

case 5:

orientation = UIImageOrientationLeftMirrored;

break;

case 7:

orientation = UIImageOrientationRightMirrored;

break;

default:

break;

}

return orientation;

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容