Make an independent copy of UIBezierPath?

I have a complex UIBezierCurve that I need to draw once with some specific line parameters, and then draw it again as an overlay with other line parameters, but I also need the last part of the curve to be slightly shorter than the previous one.

To do this, I want to create a curve on addLineToPoint: moveToPoint: to the last part, then make a copy of this curve and add the end segments of the line differently to the original and copied curves. And then I will stroke the original curve and copied.

The problem is that it does not work as I expected. I create a copy of the curve:

 UIBezierPath* copyCurve = [originalCurve copy]; 

And the drawing that I make in originalCurve after that also applies to copyCurve, so I cannot make an independent drawing for any of these curves.

What is the reason for this connection between the original and the copy and how can I get rid of it?

EDIT 1: The solution I found is to create a copy as follows:

 UIBezierPath* copyCurve=[UIBezierPath bezierPathWithCGPath:CGPathCreateMutableCopy(originalCurve.CGPath)]; 

Since this works correctly, perhaps the problem is the immutability of the copy that I get with

 [originalCurve copy] 
+7
source share
2 answers

Create a new identical path using CGPath.

 path2 = [UIBezierPath bezierPathWithCGPath:path1.CGPath]; 

The CGPath property reports that:

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

+19
source

In addition to @jrturton's answer: -

Alternatively we can use: -

  let path = UIBezierPath(ovalIn: pathRect) let newPath = path.cgPath.copy(strokingWithWidth: strokeWidth, lineCap: .butt, lineJoin: .miter, miterLimit: 0) 

enter image description here

Reference

+2
source

All Articles