Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make independent copy of UIBezierPath?

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

To do this I want to create the curve by addLineToPoint:, moveToPoint: up to the last part, then make a copy of this curve and add the final segments of the line differently in original and copied curves. And then I stroke the original curve, and the copied one.

The problem is that it doesn't work as I expected. I create a copy of the curve by:

UIBezierPath* copyCurve = [originalCurve copy];

And the drawing which I do in the originalCurve after that, is applied also to the copyCurve, so I can't do independent drawing for any of this curves.

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

EDIT 1: A solution which I've found is to create the copy in the following way:

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

Since this works properly, maybe the problem is in the immutability of the copy I get with

[originalCurve copy]
like image 941
BartoNaz Avatar asked Jul 13 '12 14:07

BartoNaz


3 Answers

copy() works fine for me as of Swift 4.

let copiedPath = originalPath.copy() as! UIBezierPath
copiedPath.addLine(...)

The originalPath does not get modified.

like image 174
Robin Stewart Avatar answered Nov 05 '22 09:11

Robin Stewart


Create a new, identical path by using the CGPath.

path2 = [UIBezierPath bezierPathWithCGPath:path1.CGPath];

The CGPath property docs state that:

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.

like image 23
jrturton Avatar answered Nov 05 '22 08:11

jrturton


In addition to @jrturton 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

like image 43
Shrawan Avatar answered Nov 05 '22 07:11

Shrawan