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

@ -14,17 +14,25 @@
+ (UIImage *)decodedImageWithImage:(UIImage *)image
{
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(image.CGImage);
BOOL imageHasAlphaInfo = (alphaInfo != kCGImageAlphaNone &&
alphaInfo != kCGImageAlphaNoneSkipFirst &&
alphaInfo != kCGImageAlphaNoneSkipLast);
CGImageRef imageRef = image.CGImage;
CGSize imageSize = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
CGRect imageRect = (CGRect){.origin = CGPointZero, .size = imageSize};
UIGraphicsBeginImageContextWithOptions(image.size, !imageHasAlphaInfo, 0);
CGRect rect = (CGRect){.origin = CGPointZero, .size = image.size};
[image drawInRect:rect];
UIImage *decompressedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
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;
}