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?

Answered by artium in 723994022

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)

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

Are you looking for a polygon intersection algorithm?

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!

Accepted Answer

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)

Is it possible to obtain the clipping path of CGContext?
 
 
Q