diff --git a/SDWebImage/SDWebImageDecoder.m b/SDWebImage/SDWebImageDecoder.m index 34af4e4..31b0cb2 100644 --- a/SDWebImage/SDWebImageDecoder.m +++ b/SDWebImage/SDWebImageDecoder.m @@ -99,16 +99,21 @@ static SDWebImageDecoder *sharedInstance; CGImageRef imageRef = image.CGImage; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); + + BOOL imageHasAlphaInfo = (alphaInfo != kCGImageAlphaNone); + + int bytesPerPixel = imageHasAlphaInfo ? 4 : 3; + CGBitmapInfo bitmapInfo = imageHasAlphaInfo ? kCGImageAlphaPremultipliedLast : kCGImageAlphaNone; + CGContextRef context = CGBitmapContextCreate(NULL, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef), 8, - // Just always return width * 4 will be enough - CGImageGetWidth(imageRef) * 4, + // Just always return width * bytesPerPixel will be enough + CGImageGetWidth(imageRef) * bytesPerPixel, // System only supports RGB, set explicitly colorSpace, - // Makes system don't need to do extra conversion when displayed. - alphaInfo | kCGBitmapByteOrder32Little); + bitmapInfo); CGColorSpaceRelease(colorSpace); if (!context) return nil;