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:
When this is run I get this in the console:
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:
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:
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.)
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.)