Reputation: 1512
Under IOS I am trying to create an image by drawing into a context created via CGBitmapContextCreate. The following is the code that I am using (which is similar to some examples that I found), but when I call it, nothing is painted. Note that 'context' in this example is obtained via a call to UIGraphicsGetCurrentContext and I can successfully paint other things into this context. Is there something that I am missing?
Thanks
CGContextRef bmContext;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
bmContext = CGBitmapContextCreate(nil, 200, 200, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
CGFloat Color[4] = {
(CGFloat) 1,
(CGFloat) 0,
(CGFloat) 0,
(CGFloat) 1 };
CGRect Rect = { { 0, 0 }, { 200, 200 } };
CGContextSetFillColor(bmContext, Color);
CGContextFillRect (bmContext, Rect);
CGImageRef image = CGBitmapContextCreateImage(bmContext);
CGContextDrawImage(context, CGRectMake(100, 100, 200, 200), image);
CGImageRelease(image);
CGContextRelease(bmContext);
CFRelease(colorSpace);
Upvotes: 2
Views: 1260
Reputation: 1512
After much experimentation I have found the problem. It appears that I need to call:
CGContextSetFillColorSpace(bmContext, colorSpace);
Otherwise it appears to be a grey scale context and is expecting only 2 values when I set the fill color, an intensity and an alpha. This means that in my example above, the alpha was always zero resulting in nothing being drawn. Setting the context to a RGB colorspace fixes the problem.
Thanks to all who took time to look at my problem.
Upvotes: 1