From 4cfb12c01cef5e4f45aa1215d38afcbb2381b594 Mon Sep 17 00:00:00 2001 From: mythodeia Date: Wed, 15 Jul 2015 10:45:31 +0300 Subject: [PATCH] fix unsupported parameter combination issues when CGBitmapContextCreate is called --- SDWebImage/SDWebImageDecoder.m | 66 ++++++++++++---------------------- 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/SDWebImage/SDWebImageDecoder.m b/SDWebImage/SDWebImageDecoder.m index 79ddb30f..b8d2fd6c 100644 --- a/SDWebImage/SDWebImageDecoder.m +++ b/SDWebImage/SDWebImageDecoder.m @@ -13,60 +13,38 @@ @implementation UIImage (ForceDecode) + (UIImage *)decodedImageWithImage:(UIImage *)image { - if (image.images) { - // Do not decode animated images - return image; - } + // do not decode animated images + if (image.images) { return image; } 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); + CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef); + BOOL anyAlpha = (alpha == kCGImageAlphaFirst || + alpha == kCGImageAlphaLast || + alpha == kCGImageAlphaPremultipliedFirst || + alpha == kCGImageAlphaPremultipliedLast); - int infoMask = (bitmapInfo & kCGBitmapAlphaInfoMask); - BOOL anyNonAlpha = (infoMask == kCGImageAlphaNone || - infoMask == kCGImageAlphaNoneSkipFirst || - infoMask == kCGImageAlphaNoneSkipLast); + if (anyAlpha) { return image; } - // CGBitmapContextCreate doesn't support kCGImageAlphaNone with RGB. - // https://developer.apple.com/library/mac/#qa/qa1037/_index.html - if (infoMask == kCGImageAlphaNone && CGColorSpaceGetNumberOfComponents(colorSpace) > 1) { - // Unset the old alpha info. - bitmapInfo &= ~kCGBitmapAlphaInfoMask; + size_t width = CGImageGetWidth(imageRef); + size_t height = CGImageGetHeight(imageRef); - // Set noneSkipFirst. - bitmapInfo |= kCGImageAlphaNoneSkipFirst; - } - // Some PNGs tell us they have alpha but only 3 components. Odd. - else if (!anyNonAlpha && CGColorSpaceGetNumberOfComponents(colorSpace) == 3) { - // Unset the old alpha info. - bitmapInfo &= ~kCGBitmapAlphaInfoMask; - bitmapInfo |= kCGImageAlphaPremultipliedFirst; - } + CGContextRef context = CGBitmapContextCreate(NULL, width, + height, + CGImageGetBitsPerComponent(imageRef), + 0, + CGImageGetColorSpace(imageRef), + kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst); - // It calculates the bytes-per-row based on the bitsPerComponent and width arguments. - CGContextRef context = CGBitmapContextCreate(NULL, - imageSize.width, - imageSize.height, - CGImageGetBitsPerComponent(imageRef), - 0, - colorSpace, - bitmapInfo); - CGColorSpaceRelease(colorSpace); - - // If failed, return undecompressed image - if (!context) return image; - - CGContextDrawImage(context, imageRect, imageRef); - CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context); + // 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]; CGContextRelease(context); + CGImageRelease(imageRefWithAlpha); - UIImage *decompressedImage = [UIImage imageWithCGImage:decompressedImageRef scale:image.scale orientation:image.imageOrientation]; - CGImageRelease(decompressedImageRef); - return decompressedImage; + return imageWithAlpha; } @end