使用 UIBezierPath 时出现错误

Having an error when using UIBezierPath

我用UIBezierPath画线。但是有一个错误 这是我的代码:

UIBezierPath *aPath = [UIBezierPath bezierPath];
[aPath moveToPoint:CGPointMake(100, 100)];
[aPath addLineToPoint:CGPointMake(200, 200)];
[aPath closePath];
aPath.lineWidth = 5;
[[UIColor redColor] setStroke];
[aPath stroke];

这是错误:

CGContextSetStrokeColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

将此赞添加到 setStrokeColor

CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);

试试这个:在 xcode 中添加符号断点到 CGPostError。 (添加符号断点,并在Symbol字段类型CGPostError

发生错误时,调试器将停止代码执行,您可以检查方法调用堆栈并检查参数。

还要确保您已导入 QuartzCode 框架。

而不是

[[UIColor redColor] setStroke];

使用

CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);

你也可以像-

一样做
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
UIBezierPath *aPath;

aPath = [UIBezierPath bezierPath];
[aPath moveToPoint:CGPointMake(0, 0)];
[aPath addLineToPoint:CGPointMake(size*2, 0)];
[aPath addLineToPoint:CGPointMake(size, size*2)];
[aPath closePath];

shapeLayer.path = aPath.CGPath;
shapeLayer.strokeColor = [[UIColor redColor] CGColor];
shapeLayer.fillColor = color;
shapeLayer.lineWidth = width;

[self addSublayer:shapeLayer];