Merge pull request #1496 from yamasakitomohiro/master

fixes  CGContextDrawImage: invalid context 0x0
https://github.com/rs/SDWebImage/issues/1401
https://github.com/rs/SDWebImage/issues/1454
https://github.com/rs/SDWebImage/issues/1457
This commit is contained in:
Konstantinos K 2016-03-17 09:46:40 +02:00
commit 1ef791bad9
1 changed files with 17 additions and 13 deletions

View File

@ -22,20 +22,17 @@
@autoreleasepool{ @autoreleasepool{
// do not decode animated images // do not decode animated images
if (image.images) { return image; } if (image.images) { return image; }
CGImageRef imageRef = image.CGImage; CGImageRef imageRef = image.CGImage;
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef); CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef);
BOOL anyAlpha = (alpha == kCGImageAlphaFirst || BOOL anyAlpha = (alpha == kCGImageAlphaFirst ||
alpha == kCGImageAlphaLast || alpha == kCGImageAlphaLast ||
alpha == kCGImageAlphaPremultipliedFirst || alpha == kCGImageAlphaPremultipliedFirst ||
alpha == kCGImageAlphaPremultipliedLast); alpha == kCGImageAlphaPremultipliedLast);
if (anyAlpha) { return image; } if (anyAlpha) { return image; }
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
// current // current
CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef)); CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef));
CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef); CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef);
@ -43,19 +40,26 @@
bool unsupportedColorSpace = (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelCMYK || imageColorSpaceModel == kCGColorSpaceModelIndexed); bool unsupportedColorSpace = (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelCMYK || imageColorSpaceModel == kCGColorSpaceModelIndexed);
if (unsupportedColorSpace) if (unsupportedColorSpace)
colorspaceRef = CGColorSpaceCreateDeviceRGB(); colorspaceRef = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, width, size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(NULL,
width,
height, height,
CGImageGetBitsPerComponent(imageRef), bitsPerComponent,
0, bytesPerRow,
colorspaceRef, colorspaceRef,
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst); kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
// Draw the image into the context and retrieve the new image, which will now have an alpha layer // 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); CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context); CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context);
UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha scale:image.scale orientation:image.imageOrientation]; UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha scale:image.scale orientation:image.imageOrientation];
if (unsupportedColorSpace) if (unsupportedColorSpace)
CGColorSpaceRelease(colorspaceRef); CGColorSpaceRelease(colorspaceRef);