From d30c2ae2095fdb6826d7c25351e2be0e25ef1940 Mon Sep 17 00:00:00 2001 From: Matej Bukovinski Date: Fri, 16 Nov 2012 14:17:25 +0100 Subject: [PATCH] Improved background image decoding performance. Tests on large images indicate an up to 4x improvement with regard to the time spent in decodedImageWithImage:. --- SDWebImage/SDWebImageDecoder.m | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/SDWebImage/SDWebImageDecoder.m b/SDWebImage/SDWebImageDecoder.m index 20eaff10..318683e7 100644 --- a/SDWebImage/SDWebImageDecoder.m +++ b/SDWebImage/SDWebImageDecoder.m @@ -13,18 +13,26 @@ @implementation UIImage (ForceDecode) + (UIImage *)decodedImageWithImage:(UIImage *)image -{ - CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(image.CGImage); - BOOL imageHasAlphaInfo = (alphaInfo != kCGImageAlphaNone && - alphaInfo != kCGImageAlphaNoneSkipFirst && - alphaInfo != kCGImageAlphaNoneSkipLast); - - UIGraphicsBeginImageContextWithOptions(image.size, !imageHasAlphaInfo, 0); - CGRect rect = (CGRect){.origin = CGPointZero, .size = image.size}; - [image drawInRect:rect]; - UIImage *decompressedImage = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); - +{ + CGImageRef imageRef = image.CGImage; + CGSize imageSize = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)); + CGRect imageRect = (CGRect){.origin = CGPointZero, .size = imageSize}; + + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); + CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); + CGContextRef context; + context = CGBitmapContextCreate(NULL, imageSize.width, imageSize.height, 8, imageSize.width * 4, colorSpace, bitmapInfo); + CGColorSpaceRelease(colorSpace); + + if (!context) return nil; + + CGContextDrawImage(context, imageRect, imageRef); + CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context); + + CGContextRelease(context); + + UIImage *decompressedImage = [UIImage imageWithCGImage:decompressedImageRef]; + CGImageRelease(decompressedImageRef); return decompressedImage; }