Question

I started learning OSX programming and try to draw a diagram programmatically. I come across examples which are either using CGPathRef (Core Graphics?) or NSBezierPath. What I lack is the understanding about these two approaches. When to use which and why? Can you help? Thanks in advance!

Was it helpful?

Solution

Think of NSBezierPath as a wrapper around CGPath which makes your life easier if you're working with views (NSView) instead of directly with the graphics context (CGContext). NSBezierPath allows you to specify the path and its drawing attributes (colours, line endings, etc) in one instance whereas if you were to use CGPath you would need to create and add the path, colours, line endings, etc separately and set the values onto the context (which is what NSBezierPath is doing for you).

So, they are similar but NSBezierPath is optimised for use when you are drawing into a view and CGPath offers all of the raw power of the graphics context.

OTHER TIPS

NSBezierPath (and it's ios counterpart UIBezierPath) are Objective-C objects that wrap much of the functionality of the CGPath API for you. This means largely that you can live in Objective-C rather than the Core Graphics C world. The biggest difference is the C version requires manual memory management and can be more verbose.

You can use whichever you prefer unless another function or method requires a specific one.

It is however helpful to learn Cire Graphics to understand what really goes on in Cocoa drawing.

Well in some scenarios NSBezierPath is more beneficial than CGPathRef for example in NSBezierpath it provides the api which can draw the path using color but CGPathRef does not provide any api to draw the path for drawing the same need to use CGContext. This is the one difference and second difference is CGPath derived from ApplicationService framework and NSBezierPath derived from Appkit framwork

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top