Search code examples
objective-ccocoadrawingcopyuibezierpath

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]

Solution

  • 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.