Error drawing UIBezierPath. No current point

I create UIBezierPathas follows:

UIBezierPath *path = [UIBezierPath bezierPath];

[path addLineToPoint:CGPointMake(200.0, 40.0)];
[path addLineToPoint:CGPointMake(160, 140)];
[path addLineToPoint:CGPointMake(40.0, 140)];
[path addLineToPoint:CGPointMake(0.0, 40.0)];
[path closePath];

The problem is that when I try to draw mine UIBezierPath, it does not appear, and I get an error message:

<Error>: void CGPathCloseSubpath(CGMutablePathRef): no current point.

My drawing code is very simple:

UIGraphicsBeginImageContext(self.view.bounds.size);

[[UIColor whiteColor] set];
[myPath stroke];

UIGraphicsEndImageContext();

What am I doing wrong?

+3
source share
1 answer

The error message is cleared. One line of code was missing when I created my own UIBezierPath:

[path moveToPoint:CGPointMake(0, 0)];

I added it, and everything worked like a charm.

UIBezierPath *path = [UIBezierPath bezierPath];

[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(160, 140)];
[path addLineToPoint:CGPointMake(40.0, 140)];
[path addLineToPoint:CGPointMake(0.0, 40.0)];
[path closePath]

Interestingly, the error occurred when I tried to draw a line, and not earlier - when I created it.

+5
source

All Articles