The source color space for the specified bitmap image, or NULL if the image is an image mask\"" />
开发者

CGImageGetColorSpace and image mask

As reported from documentation from apple:

"CGImageGetColorSpace-> The source color space for the specified bitmap image, or NULL if the image is an image mask"

I need to create an image mask, tried with black and white bitmap and many other format, but the function returns like the image is a RGB (without alpha).

What kind of image can I pass to have CGImageGetColorSpace return NULL (image mask)?

If I make a grayscale image and loaded as were an alpha channel (creating context as 8bit/grayscale) the image is totally white.

Any ideas?

This is the code of example "Texture2D":

@implementation Texture2D (Image)

- (id) initWithImage:(UIImage *)uiImage
{
NSUInteger    width,
   height,
   i;
CGContextRef   context = nil;
void*     data = nil;;
CGColorSpaceRef   colorSpace;
void*     tempData;
unsigned int*   inPixel32;
unsigned short*   outPixel16;
BOOL     hasAlpha;
CGI开发者_JAVA技巧mageAlphaInfo  info;
CGAffineTransform  transform;
CGSize     imageSize;
Texture2DPixelFormat    pixelFormat;
CGImageRef    image;
UIImageOrientation  orientation;
BOOL     sizeToFit = NO;


image = [uiImage CGImage];
orientation = [uiImage imageOrientation]; 

if(image == NULL) {
[self release];
NSLog(@"Image is Null");
return nil;
}


info = CGImageGetAlphaInfo(image);
hasAlpha = ((info == kCGImageAlphaPremultipliedLast) || (info ==    CGImageAlphaPremultipliedFirst) || (info == kCGImageAlphaLast) || (info == kCGImageAlphaFirst) ? YES : NO);
if(CGImageGetColorSpace(image)) {
if(hasAlpha)
pixelFormat = kTexture2DPixelFormat_RGBA8888;
else
pixelFormat = kTexture2DPixelFormat_RGB565;
} else  //NOTE: No colorspace means a mask image
pixelFormat = kTexture2DPixelFormat_A8;


CGImageRef maskRef = [uiImage CGImage];
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
        CGImageGetHeight(maskRef),
        CGImageGetBitsPerComponent(maskRef),
        CGImageGetBitsPerPixel(maskRef),
        CGImageGetBytesPerRow(maskRef),
        CGImageGetDataProvider(maskRef), NULL, false);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜