0

I'm looking to make an inverted clipping CGPath based on an input CGPath shape on macOS. My plan is to simply make a sufficiently big simple rect and then add a reversed order version of the input path into it to make the hole, based on the winding direction rule.

The problem is that I can't seem to find any way to reverse the CGPath. On iOS there seems to be a bezierPathByReversingPath for UIBezierPath, but I can't find anything like it on macOS (EDIT: turns out this was a mistake on my part, NSBezierPath has a corresponding method). Does anyone know how to reverse the point order / drawing direction of an existing CGPath?

I found this old thread, from eight years ago asking for more or less the same thing, but there seem to be no solutions there for macOS except for one, where the links are unfortunately no longer working and I'm not allowed to comment. How to reverse the point order of a CGPath So I'm hoping for better luck this time.

Lostminds
  • 11
  • 3
  • 1
    `bezierPathByReversingPath` exists on `NSBezierPath`. – Larme Oct 30 '19 at 17:42
  • Wow, that's really helpful, not sure how I missed that. As I still need CGPaths it would still be good to have a cleaner approach using only CGPaths, but I guess converting the CGPath to a NSBezierPath, reversing that and then converting that back to a CGPath could be possible. – Lostminds Oct 31 '19 at 19:09

1 Answers1

0

Although not very elegant, in then end I did this by going via NSBezierPath. In these steps:

  1. Make a NSBezierPath based on the CGPath, using CGPathApply to get access to the elements in the CGPath one by one and add the corresponding ones to the NSBezierPath. (For example like this Convert CGPathRef to NSBezierPath)

  2. Use bezierPathByReversingPath on the NSBezierPath to reverse the point order.

  3. Get the reversed CGPath back from the NSBezierPath by constructing a new CGPath out of the elements in the NSBezierpath (for example like this: How can i convert NSBezierPath to CGPath)

Lostminds
  • 11
  • 3