Improved background image decoding performance.

Tests on large images indicate an up to 4x improvement with regard to the time spent in decodedImageWithImage:.
This commit is contained in:
Matej Bukovinski 2012-11-16 14:17:25 +01:00 committed by Olivier Poitrey
parent 022aa2146e
commit d30c2ae209
1 changed files with 20 additions and 12 deletions

View File

@ -13,18 +13,26 @@
@implementation UIImage (ForceDecode) @implementation UIImage (ForceDecode)
+ (UIImage *)decodedImageWithImage:(UIImage *)image + (UIImage *)decodedImageWithImage:(UIImage *)image
{ {
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(image.CGImage); CGImageRef imageRef = image.CGImage;
BOOL imageHasAlphaInfo = (alphaInfo != kCGImageAlphaNone && CGSize imageSize = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
alphaInfo != kCGImageAlphaNoneSkipFirst && CGRect imageRect = (CGRect){.origin = CGPointZero, .size = imageSize};
alphaInfo != kCGImageAlphaNoneSkipLast);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UIGraphicsBeginImageContextWithOptions(image.size, !imageHasAlphaInfo, 0); CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGRect rect = (CGRect){.origin = CGPointZero, .size = image.size}; CGContextRef context;
[image drawInRect:rect]; context = CGBitmapContextCreate(NULL, imageSize.width, imageSize.height, 8, imageSize.width * 4, colorSpace, bitmapInfo);
UIImage *decompressedImage = UIGraphicsGetImageFromCurrentImageContext(); CGColorSpaceRelease(colorSpace);
UIGraphicsEndImageContext();
if (!context) return nil;
CGContextDrawImage(context, imageRect, imageRef);
CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *decompressedImage = [UIImage imageWithCGImage:decompressedImageRef];
CGImageRelease(decompressedImageRef);
return decompressedImage; return decompressedImage;
} }