Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when drawing a UIBezierPath. No current point

I am creating a UIBezierPath like this:

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 my UIBezierPath, it does not appear and I get an error:

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

My drawing code is very basic:

UIGraphicsBeginImageContext(self.view.bounds.size);

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

UIGraphicsEndImageContext();

What am I doing wrong?

like image 281
Rafa de King Avatar asked Mar 21 '23 12:03

Rafa de King


1 Answers

The error message is clear. One line of code was missing when I was creating my 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]

It is interesting that the error happened when I was trying to draw the line and not before - when I created it.

like image 96
Rafa de King Avatar answered Apr 01 '23 09:04

Rafa de King