From fb62c3fde1bb604cc92541a398573aaac67009eb Mon Sep 17 00:00:00 2001 From: mythodeia Date: Fri, 17 Jul 2015 00:11:54 +0300 Subject: [PATCH] better handling of colorspace models --- SDWebImage/SDWebImageDecoder.m | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/SDWebImage/SDWebImageDecoder.m b/SDWebImage/SDWebImageDecoder.m index b8d2fd6c..9527603b 100644 --- a/SDWebImage/SDWebImageDecoder.m +++ b/SDWebImage/SDWebImageDecoder.m @@ -29,11 +29,17 @@ size_t width = CGImageGetWidth(imageRef); size_t height = CGImageGetHeight(imageRef); + // default RGB + CGColorSpaceRef RGBcolorSpace = CGColorSpaceCreateDeviceRGB(); + + // current + CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef)); + CGContextRef context = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 0, - CGImageGetColorSpace(imageRef), + (imageColorSpaceModel == 0 || imageColorSpaceModel == -1) ? RGBcolorSpace : CGImageGetColorSpace(imageRef), kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst); // Draw the image into the context and retrieve the new image, which will now have an alpha layer @@ -41,9 +47,10 @@ CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context); UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha]; + CGColorSpaceRelease(RGBcolorSpace); CGContextRelease(context); CGImageRelease(imageRefWithAlpha); - + return imageWithAlpha; }