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:
parent
022aa2146e
commit
d30c2ae209
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue