diff --git a/SDWebImage/SDWebImageDecoder.m b/SDWebImage/SDWebImageDecoder.m index a7c02468..7a140dcf 100644 --- a/SDWebImage/SDWebImageDecoder.m +++ b/SDWebImage/SDWebImageDecoder.m @@ -22,20 +22,17 @@ @autoreleasepool{ // do not decode animated images if (image.images) { return image; } - + CGImageRef imageRef = image.CGImage; - + CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef); BOOL anyAlpha = (alpha == kCGImageAlphaFirst || alpha == kCGImageAlphaLast || alpha == kCGImageAlphaPremultipliedFirst || alpha == kCGImageAlphaPremultipliedLast); - + if (anyAlpha) { return image; } - - size_t width = CGImageGetWidth(imageRef); - size_t height = CGImageGetHeight(imageRef); - + // current CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef)); CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef); @@ -43,19 +40,26 @@ bool unsupportedColorSpace = (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelCMYK || imageColorSpaceModel == kCGColorSpaceModelIndexed); if (unsupportedColorSpace) 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, - CGImageGetBitsPerComponent(imageRef), - 0, + bitsPerComponent, + bytesPerRow, colorspaceRef, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst); - + // 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 scale:image.scale orientation:image.imageOrientation]; - + if (unsupportedColorSpace) CGColorSpaceRelease(colorspaceRef);