ios - 使用 UIBezierPath 时出现错误

标签 ios objective-c cgcontext 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.

最佳答案

而不是

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

关于ios - 使用 UIBezierPath 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30024305/

相关文章:

ios - 在 iOS 中添加 PDF 页面

ios - 在代码中自定义 UIView

objective-c - 从 NSString 中动态附加和删除子字符串

objective-c - 如何将 NSInteger 转换为二进制(字符串)值

ios - UISearchDisplayController CGContext 错误 iOS 7

ios - 如何使用SDWebImage在cgcontext中绘制图像

iphone - 无法从 iPhone 连接基于 soap 的 wcf 服务

ios - UIImageView 无法显示图像

iphone - didUpdateToLocation 未调用

iphone - 使用 Core Motion 和 CATransform3D 旋转图像