Wednesday, June 5, 2013

UIBezierPath bezierPathWithCGPath example in Objective C (iOS).


UIBezierPath bezierPathWithCGPath

Creates and returns a new UIBezierPath object initialized with the contents of a Core Graphics path.

+ (UIBezierPath *)bezierPathWithCGPath:(CGPathRef)CGPath

Parameters
CGPath
The Core Graphics path from which to obtain the initial path information. If this parameter is nil, the method raises an exception.

Return Value of [UIBezierPath bezierPathWithCGPath]
A new path object with the specified path information.

UIBezierPath bezierPathWithCGPath example.
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.

Example of [UIBezierPath bezierPathWithCGPath].
The easiest way if you can target iOS 3.2 or higher is to wrap the CGPathRef in a UIBezierPath:

[myArray addObject:[UIBezierPath bezierPathWithCGPath:myPath]];
If you need to support earlier iOS versions, you can use a CFArray instead of NSArray because CGPath derives from CFType.

UIBezierPath bezierPathWithCGPath example.
// this method will let you easily select a bezier path ( 15 px up and down of a path drawing)
- (UIBezierPath *)tapTargetForPath:(UIBezierPath *)path
{
    if (path == nil) {
        return nil;
    }

    CGPathRef tapTargetPath = CGPathCreateCopyByStrokingPath(path.CGPath, NULL, fmaxf(35.0f, path.lineWidth), path.lineCapStyle, path.lineJoinStyle, path.miterLimit);
    if (tapTargetPath == NULL) {
        return nil;
    }

    UIBezierPath *tapTarget = [UIBezierPath bezierPathWithCGPath:tapTargetPath];
    CGPathRelease(tapTargetPath);
    return tapTarget;
}

End of UIBezierPath bezierPathWithCGPath example article.