Creating a mask from a graphics context

Posted by Magic Bullet Dave on Stack Overflow See other posts from Stack Overflow or by Magic Bullet Dave
Published on 2010-04-27T12:15:30Z Indexed on 2010/05/19 6:00 UTC
Read the original article Hit count: 340

I want to be able to create a greyscale image with no alpha from a png in the app bundle.

This works, and I get an image created:

// Create graphics context the size of the overlapping rectangle
UIGraphicsBeginImageContext(rectangleOfOverlap.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();

// More stuff

CGContextDrawImage(ctx, drawRect2, [UIImage imageNamed:@"Image 01.png"].CGImage);

// Create the new UIImage from the context
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

However the resulting image is 32 bits per pixel and has an alpha channel, so when I use CGCreateImageWithMask it doesn't work. I've tried creating a bitmap context thus:

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef ctx =CGBitmapContextCreate(nil, rectangleOfOverlap.size.width, rectangleOfOverlap.size.height, 8, rectangleOfOverlap.size.width , colorSpace, kCGImageAlphaNone);

UIGraphicsGetImageFromCurrentImageContext returns zero and the resulting image is not created. Am I doing something dumb here?

Any help would be greatly appreciated.

Regards

Dave

© Stack Overflow or respective owner

Related posts about iphone

Related posts about core-graphics