Is it possible to obtain the clipping path of CGContext?

There is no API to create an intersection between two CGPaths, however CoreGraphics knows how to do it behind the scenes. When calling CGContextClip (link) it will intersect current and clipping paths and store it in the clipping path.

I was thinking to utilize this to perform intersections between paths I have. The problem is I can not find a way to retrieve back the clipping path from CGContext.

Am I correct that such API does not exist or did I miss something?

Accepted Reply

It looks like what I need was added in iOS 16 beta https://developer.apple.com/documentation/coregraphics/cgpath/3994964-intersection?changes=latest_minor&language=_5 So I guess I was right and currently it is not available.

However for my particular case, I did not need the intersection path itself, only to stroke it so a colleague suggested to clip one path by the other and stroke the result and then switch roles and stroke the other part. Together the intersection path is approximately stroked (with some visual artifacts at intersection points)

  • From these new CGPath functionalities that were added in iOS 16.0, I have tested componentsSeparated and intersection. And in my experience they do not work. In my testing with somewhat more complex paths (self-intersecting paths with 5 segments or so), they give the wrong result for every (!) example that I randomly constructed. Has anyone else made a similar observation? 

     

Add a Comment

Replies

I believe you are correct that there is no way to retrieve the clipping path.

Are you looking for a polygon intersection algorithm?

  • Not a polygon intersection as CGPath is not a polygon. There is actually a third party library that does this, but I was hoping to find a solution with using only the standard libraries Apple provide (since Core Graphics already have to perform this intersection under the hood).

Add a Comment

I was hoping to find a solution with using only the standard libraries Apple provide

Bad idea IMO. Apple's code is (a) closed source, so you can't debug it yourself, (b) not well supported; if you find a bug it will at best be fixed with a future OS update in at least a year's time, with no visibility about whether a fix is actually being worked on, and (c) not portable to other platforms. If you've found another library that does what you want, just use it!

It looks like what I need was added in iOS 16 beta https://developer.apple.com/documentation/coregraphics/cgpath/3994964-intersection?changes=latest_minor&language=_5 So I guess I was right and currently it is not available.

However for my particular case, I did not need the intersection path itself, only to stroke it so a colleague suggested to clip one path by the other and stroke the result and then switch roles and stroke the other part. Together the intersection path is approximately stroked (with some visual artifacts at intersection points)

  • From these new CGPath functionalities that were added in iOS 16.0, I have tested componentsSeparated and intersection. And in my experience they do not work. In my testing with somewhat more complex paths (self-intersecting paths with 5 segments or so), they give the wrong result for every (!) example that I randomly constructed. Has anyone else made a similar observation? 

     

Add a Comment