You can't. CGPathCreateCopyByStrokingPath()
is the way to create a path that represents what would be drawn if some other path were stroked (i.e. drawn) with the relevant drawing parameters. Why are you searching for a different way?
There's a need to create a new path because that new path is a different path from the original. A path is conceptually a geometric object consisting of "mathematical" lines and curves. Those lines and curves have no width. Line width is conceptually a drawing parameter, not a property of the path. (This is confusing because both UIBezierPath
and NSBezierPath
have drawing related properties. Those are just a convenience. The CGContext
API makes the separation clearer. It would be more consistent but less convenient if line width were set on the current context, like the color is.)
The cgpath
in your code snippet is a line. The path created by stroking that, assuming either butt or square line cap, would be a rectangle. Those are two very different paths. You have to create the second one to test if a point is within it. Even if there were an API to do what you want directly, the framework would still have to compute the second path behind the scenes to answer the question.