Problem with CGBitmapContextCreateImage() in Xcode (Mac OS X app)

Status
Not open for further replies.

theJeff

Ars Scholae Palatinae
1,109
I'm "porting" my iOS application to run in Mac OS X and I'm having a problem with using CGBitmapContextCreateImage() in my NSView's drawRect method.

My view is subclasses from NSView and here is the relevant code in the drawRect method:

Code:
- (void)drawRect:(NSRect)dirtyRect
{
        CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];

	[backgroundImage drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
	CGContextSetRGBFillColor (context, 1.0, 1.0, 1.0, 0.75);
	
	CGContextSaveGState(context);
	CGContextTranslateCTM(context, 0, backgroundBaseline);
	CGContextScaleCTM(context, viewWidth / distance, viewWidth / distance);
	CGContextAddPath(context, backgroundPathRef);
	CGContextDrawPath(context, kCGPathFill);
	CGContextRestoreGState(context);
	
	CGImageRef backgroundBitmapRef = CGBitmapContextCreateImage(context);
....

When this is run I get this in the console:
Code:
<Error>: CGBitmapContextCreateImage: invalid context 0x11d030

I've done plenty of googling and haven't found any fruitful information. In the iOS version of this app, I subclass UIView and use:
Code:
	CGContextRef context = UIGraphicsGetCurrentContext();

to get the current context. Works fine.

Is there something simple I'm overlooking? Why is that not a valid context? I try to run:
Code:
	CGBitmapInfo info = CGBitmapContextGetBitmapInfo (context);

to get some info on the context but that generates errors as well. I'm able to draw in this context with NSImages and CGPaths but I can't seem to generate a CGImageRef from the current context (which I need to do.)
 

Mark Cianciosa

Wise, Aged Ars Veteran
161
That just means that your UIView just happened to be backed by a bitmap context. I don't think you can always count on that. For instance if you were drawing to a coreanimation layer, which I think most UIVeiws in iOS do, those are backed by a bitmap because that bitmap is then rendered as a texture using OpenGL. But in the case of your Mac OS X view it's probably drawing directly to some context associated with the screen that isn't based on a bitmap.
 
Status
Not open for further replies.