CGContext vs CALayer

后端 未结 1 1542
夕颜
夕颜 2020-12-30 16:47

I\'m trying to get a handle on Quartz 2D and looking at the online book Quartz 2D Graphics for Mac OS X Developers.

One very basic thing that is confusing me is the

相关标签:
1条回答
  • 2020-12-30 17:21

    Yes, the CAShapeLayer will draw the path into a context for you.

    Note that with CAShapeLayer, you use CGPath, whereas if you're drawing into a context yourself, you may want to use CGContext's own path-drawing functions instead.

    The biggest difference is that the CGContext path functions add commands directly into the context's current path, to be thrown away when you tell the context to fill or stroke (or explicitly abandon) the path, whereas a CGPath object keeps them around to be reused again and again—e.g., if you change the CAShapeLayer's drawing properties.

    Of course, there are cases for using CGPath even when you're going to draw it into a context. If the set of commands never changes, then it's a potential optimization; it makes transforming the path easy; you can iterate on its commands (e.g., to export them to a file) afterward. You can also write the plotting code with UIBezierPath (i.e., with Objective-C messages) and then ask it for a CGPath.

    Using CGContext in either way isn't wrong, and there probably are cases in which it's best, but CAShapeLayer is the easiest way for most purposes, particularly in Cocoa Touch, where every UIView is backed by a CALayer that you could put the CAShapeLayer into.

    0 讨论(0)
提交回复
热议问题