better handling of colorspace models

This commit is contained in:
mythodeia 2015-07-17 00:11:54 +03:00
parent 25b4c178d9
commit fb62c3fde1
1 changed files with 9 additions and 2 deletions

View File

@ -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,6 +47,7 @@
CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context);
UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha];
CGColorSpaceRelease(RGBcolorSpace);
CGContextRelease(context);
CGImageRelease(imageRefWithAlpha);