绘制UIBezierPath时出错。没有当前的观点

时间:2014-02-22 15:18:35

标签: ios objective-c

我正在创建一个UIBezierPath,如下所示:

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];

问题在于,当我尝试绘制UIBezierPath时,它没有出现,我收到错误:

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

我的绘图代码非常基本:

UIGraphicsBeginImageContext(self.view.bounds.size);

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

UIGraphicsEndImageContext();

我做错了什么?

1 个答案:

答案 0 :(得分:5)

错误消息很明确。在创建UIBezierPath时,缺少一行代码:

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

我添加了它,一切都像魅力一样。

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]

有趣的是,当我尝试绘制线时发生错误,而不是之前 - 当我创建它时。