Reputation: 5698
I just started learning OSX programming, and I've stumbled upon a problem where I don't know if I should use CGContextRef
or NSGraphicsContext
.
What is the difference?
I barely know the difference between CG or NS [and how they relate to cocoa] (I've tried googling to no avail)
Could anybody shed some light on when I would want to use either, or what the key differences are?
I'd take a link to an article, even. I just can't find anything from my web searches.
I know I'm asking a lot of questions in one, so thanks in advance!
Upvotes: 4
Views: 1185
Reputation: 1026
CG based commands use a C like syntax. NS based commands use the Objective-c style interface;
- (void)drawRect:(NSRect)nr
{
NSGraphicsContext* gc = [NSGraphicsContext currentContext];
CGContextRef cggc = [gc CGContext];
CGPathRef cgp = CGPathCreateMutable();
// do CGPath stuff
CGContextFlush(cggc);
NSBezierPath* nzb = [[NSBezierPath alloc] init];
// Do NSPath stuff
[gc flushGraphics];
}
Upvotes: 0
Reputation: 5698
For anybody who's reading this, NSGraphicsContext layers on top of CGContextRef; and similar to most CG<->NS relations, the NS counterpart mirrors the CG's basic functionality.
But NSGraphicsContext allows you to do a little more, such as 'graphicsContextWithGraphicsPort' to create context from a graphics port.
Upvotes: 2