Skip to content

Commit fb62c3f

Browse files
committed
better handling of colorspace models
1 parent 25b4c17 commit fb62c3f

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

SDWebImage/SDWebImageDecoder.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,28 @@ + (UIImage *)decodedImageWithImage:(UIImage *)image {
2929
size_t width = CGImageGetWidth(imageRef);
3030
size_t height = CGImageGetHeight(imageRef);
3131

32+
// default RGB
33+
CGColorSpaceRef RGBcolorSpace = CGColorSpaceCreateDeviceRGB();
34+
35+
// current
36+
CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef));
37+
3238
CGContextRef context = CGBitmapContextCreate(NULL, width,
3339
height,
3440
CGImageGetBitsPerComponent(imageRef),
3541
0,
36-
CGImageGetColorSpace(imageRef),
42+
(imageColorSpaceModel == 0 || imageColorSpaceModel == -1) ? RGBcolorSpace : CGImageGetColorSpace(imageRef),
3743
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
3844

3945
// Draw the image into the context and retrieve the new image, which will now have an alpha layer
4046
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
4147
CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context);
4248
UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha];
4349

50+
CGColorSpaceRelease(RGBcolorSpace);
4451
CGContextRelease(context);
4552
CGImageRelease(imageRefWithAlpha);
46-
53+
4754
return imageWithAlpha;
4855
}
4956

0 commit comments

Comments
 (0)