From 65fbc3b85a29bd3d27343aedb7875e4039e07e6c Mon Sep 17 00:00:00 2001 From: lagapollo Date: Wed, 7 Oct 2015 17:20:45 +0200 Subject: [PATCH] Memory usage upgrade autorelease the bitmap context for clearing cache when memory warnings --- SDWebImage/SDWebImageDecoder.m | 88 ++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/SDWebImage/SDWebImageDecoder.m b/SDWebImage/SDWebImageDecoder.m index 39970891..22b7727e 100644 --- a/SDWebImage/SDWebImageDecoder.m +++ b/SDWebImage/SDWebImageDecoder.m @@ -13,49 +13,57 @@ @implementation UIImage (ForceDecode) + (UIImage *)decodedImageWithImage:(UIImage *)image { - // do not decode animated images - if (image.images) { return image; } - - CGImageRef imageRef = image.CGImage; - - CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef); - BOOL anyAlpha = (alpha == kCGImageAlphaFirst || - alpha == kCGImageAlphaLast || - alpha == kCGImageAlphaPremultipliedFirst || - alpha == kCGImageAlphaPremultipliedLast); - - if (anyAlpha) { return image; } - - size_t width = CGImageGetWidth(imageRef); - size_t height = CGImageGetHeight(imageRef); - - // current - CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef)); - CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef); + // while downloading huge amount of images + // autorelease the bitmap context + // and all vars to help system to free memory + // when there are memory warning. + // on iOS7, do not forget to call + // [[SDImageCache sharedImageCache] clearMemory]; + @autoreleasepool{ + // do not decode animated images + if (image.images) { return image; } - bool unsupportedColorSpace = (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelIndexed); - if (unsupportedColorSpace) - colorspaceRef = CGColorSpaceCreateDeviceRGB(); - - CGContextRef context = CGBitmapContextCreate(NULL, width, - height, - CGImageGetBitsPerComponent(imageRef), - 0, - colorspaceRef, - kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst); - - // Draw the image into the context and retrieve the new image, which will now have an alpha layer - CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); - CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context); - UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha]; - - if (unsupportedColorSpace) - CGColorSpaceRelease(colorspaceRef); + CGImageRef imageRef = image.CGImage; - CGContextRelease(context); - CGImageRelease(imageRefWithAlpha); + CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef); + BOOL anyAlpha = (alpha == kCGImageAlphaFirst || + alpha == kCGImageAlphaLast || + alpha == kCGImageAlphaPremultipliedFirst || + alpha == kCGImageAlphaPremultipliedLast); - return imageWithAlpha; + if (anyAlpha) { return image; } + + size_t width = CGImageGetWidth(imageRef); + size_t height = CGImageGetHeight(imageRef); + + // current + CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef)); + CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef); + + bool unsupportedColorSpace = (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelIndexed); + if (unsupportedColorSpace) + colorspaceRef = CGColorSpaceCreateDeviceRGB(); + + CGContextRef context = CGBitmapContextCreate(NULL, width, + height, + CGImageGetBitsPerComponent(imageRef), + 0, + colorspaceRef, + kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst); + + // Draw the image into the context and retrieve the new image, which will now have an alpha layer + CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); + CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context); + UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha]; + + if (unsupportedColorSpace) + CGColorSpaceRelease(colorspaceRef); + + CGContextRelease(context); + CGImageRelease(imageRefWithAlpha); + + return imageWithAlpha; + } } @end