Skip to content

Commit d00d368

Browse files
committed
Fix unsupported colorspace issue.
Without this fix, this url: https://abs.twimg.com/sticky/default_profile_images/default_profile_3_normal.png isn't correctly decoded and the method returns a nil image.. perhaps there should be a failsafe that checks the return value and returns the input image instead if the return value is nil.
1 parent 49f6e53 commit d00d368

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)