From 0c1dd3c857fcf4d704baaed0d0619c0ea3dd07ac Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Mon, 12 Nov 2012 00:05:19 +0100 Subject: [PATCH] Fix CGBitmapContextCreate errors (fix #204) --- SDWebImage/SDWebImageDecoder.m | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/SDWebImage/SDWebImageDecoder.m b/SDWebImage/SDWebImageDecoder.m index f09810a1..20eaff10 100644 --- a/SDWebImage/SDWebImageDecoder.m +++ b/SDWebImage/SDWebImageDecoder.m @@ -14,36 +14,17 @@ + (UIImage *)decodedImageWithImage:(UIImage *)image { - CGImageRef imageRef = image.CGImage; - CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); - CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); - + CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(image.CGImage); BOOL imageHasAlphaInfo = (alphaInfo != kCGImageAlphaNone && alphaInfo != kCGImageAlphaNoneSkipFirst && alphaInfo != kCGImageAlphaNoneSkipLast); - int bytesPerPixel = alphaInfo != kCGImageAlphaNone ? 4 : 3; - CGBitmapInfo bitmapInfo = imageHasAlphaInfo ? kCGImageAlphaPremultipliedLast : alphaInfo; + UIGraphicsBeginImageContextWithOptions(image.size, !imageHasAlphaInfo, 0); + CGRect rect = (CGRect){.origin = CGPointZero, .size = image.size}; + [image drawInRect:rect]; + UIImage *decompressedImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); - CGContextRef context = CGBitmapContextCreate(NULL, - CGImageGetWidth(imageRef), - CGImageGetHeight(imageRef), - 8, - // Just always return width * bytesPerPixel will be enough - CGImageGetWidth(imageRef) * bytesPerPixel, - // System only supports RGB, set explicitly - colorSpace, - bitmapInfo); - CGColorSpaceRelease(colorSpace); - if (!context) return image; - - CGRect rect = (CGRect){CGPointZero,{CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)}}; - CGContextDrawImage(context, rect, imageRef); - CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context); - CGContextRelease(context); - - UIImage *decompressedImage = [[UIImage alloc] initWithCGImage:decompressedImageRef scale:image.scale orientation:image.imageOrientation]; - CGImageRelease(decompressedImageRef); return decompressedImage; }