Skip to content

Commit 5a5c65d

Browse files
committed
Merge pull request SDWebImage#1268 from izackp/patch-1
Fix unsupported colorspace issue.
2 parents 49f6e53 + d00d368 commit 5a5c65d

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

SDWebImage/SDWebImageDecoder.m

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,29 @@ + (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-
3532
// current
3633
CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef));
34+
CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef);
35+
36+
bool unsupportedColorSpace = (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelIndexed);
37+
if (unsupportedColorSpace)
38+
colorspaceRef = CGColorSpaceCreateDeviceRGB();
3739

3840
CGContextRef context = CGBitmapContextCreate(NULL, width,
3941
height,
4042
CGImageGetBitsPerComponent(imageRef),
4143
0,
42-
(imageColorSpaceModel == 0 || imageColorSpaceModel == -1) ? RGBcolorSpace : CGImageGetColorSpace(imageRef),
44+
colorspaceRef,
4345
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
4446

4547
// Draw the image into the context and retrieve the new image, which will now have an alpha layer
4648
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
4749
CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context);
4850
UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha];
4951

50-
CGColorSpaceRelease(RGBcolorSpace);
52+
if (unsupportedColorSpace)
53+
CGColorSpaceRelease(colorspaceRef);
54+
5155
CGContextRelease(context);
5256
CGImageRelease(imageRefWithAlpha);
5357

0 commit comments

Comments
 (0)