2015-05-04 33 views
0

我使用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.

+1

http://stackoverflow.com/questions/19599266/invalid-context -0x0-under-ios-7-0-and-system-degradation –

回答

1

不是

[[UIColor redColor] setStroke]; 

使用

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

你也可以做同样的AS-

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

我用你的第二种方式。有效。非常感谢你 – thinhvd

+0

@thinhvd omit [aPath fill]; –

0

加入这个喜欢setStrokeColor

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

+0

它不起作用 – thinhvd

+0

你用'[[UIColor redColor] setStro替换了它吗ke];'? – Mrunal

+0

是的,我做过。但仍然有错误 – thinhvd

0

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

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

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