Get the UIBezierPath or CGPath from CGContextRef

I have a bezier path inside the current graphics context that I want to store as a UIBezierPath or CGPath. Do you know how to get the path out of a CGContextRef?


You can copy the context's current path using CGContextCopyPath.

There is no function that copies the clipping path. Indeed, the clipping region won't be defined by a path if you've used CGContextClipToMask.

This property contains a snapshot of the path at any given point in time. Getting this property returns an immutable path object that you can pass to Core Graphics functions.


You can use the CGContextCopyPath function:

CGPathRef myPath = CGContextCopyPath(context);

If you have existing paths made using UIBezierPath or CGPath it’s trivial to convert them for use in SwiftUI because the Path struct has an initializer directly from CGPath. Note: UIBezierPath is not available in macOS, so if you’re trying to keep your SwiftUI code cross platform you should migrate to CGPath instead.


Swift 4.0 and higher

You can just use for coping the path.

let copy = context.path

Because a UIBezierPath object owns its underlying CGPathRef data type, you cannot simply retrieve that type and modify it directly. Instead, you must make a mutable copy, modify the copy, and then assign the copy back to the CGPath property as shown in Listing 2-4 .


Questions: I want to get the image from the UIBezierpath closed path(See the image). I draw the image on the UIView using drawRect method and also draw the lines are using the drawRect method. How can I get the particular closed path image? Please help me. Thanks in advance. This code used for draw the


We'll arbitrarily start in the bottom left corner and work clockwise. I'll use the grid in the image to get the x and y values for the points. I'll hardcode everything here, but of course you wouldn't do that in a real project. The basic process is: Create a new UIBezierPath; Choose a starting point on the path with moveToPoint; Add segments to


ios - with - uibezierpath gradient swift Draw gradient along a curved UIBezierPath (2) In an app, I draw a curved UIBezierPath an an MKOverlayPathView class to show flight routes.