尝试使用 UIBezierPath 绘制时收到错误
我正在尝试在自定义 UIView 中画一些线。
据我所知,为了节省 CoreGraphics 的麻烦,我可以使用 UIBezierPath (我在 Mac 上对 NSBezierPath 做了类似的事情)。 我有一些代码试图绘制线条,但出现输出错误,并且找不到一些示例代码的合适参考来说明发生了什么,有什么想法吗? 下面的代码...
代码:
- (void)drawRect:(CGRect)rect {
// Drawing code
UIBezierPath *line1 = [UIBezierPath bezierPath];
[[UIColor blackColor] setStroke];
[line1 setLineWidth:3];
[line1 moveToPoint:CGPointMake(0, 0)];
[line1 addLineToPoint:CGPointMake(320, 480)];
[line1 stroke];
}
错误:
Sat Oct 2 19:26:43 mercury.config mobileManual[46994] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
更新:这是当前的代码,没有错误但也没有绘图..想法?
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor yellowColor]];
[self.view setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
UIBezierPath *line1 = [UIBezierPath bezierPath];
[line1 setLineWidth:3];
[line1 moveToPoint:CGPointMake(0, 0)];
[line1 addLineToPoint:CGPointMake(320, 480)];
[line1 stroke];
CGContextRestoreGState(context);
}
I'm trying to draw some lines inside my custom UIView.
From what I can see, to save messing around with CoreGraphics, I can use UIBezierPath (i've done similar with NSBezierPath on the Mac).
I have some code that tries to draw the lines but I get output errors and can't find a decent reference with some sample code to illustrate whats going on, any ideas?
Code below...
Code:
- (void)drawRect:(CGRect)rect {
// Drawing code
UIBezierPath *line1 = [UIBezierPath bezierPath];
[[UIColor blackColor] setStroke];
[line1 setLineWidth:3];
[line1 moveToPoint:CGPointMake(0, 0)];
[line1 addLineToPoint:CGPointMake(320, 480)];
[line1 stroke];
}
Errors:
Sat Oct 2 19:26:43 mercury.config mobileManual[46994] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
UPDATE: Here's the current code, No errors but also no drawing.. ideas?
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor yellowColor]];
[self.view setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
UIBezierPath *line1 = [UIBezierPath bezierPath];
[line1 setLineWidth:3];
[line1 moveToPoint:CGPointMake(0, 0)];
[line1 addLineToPoint:CGPointMake(320, 480)];
[line1 stroke];
CGContextRestoreGState(context);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就像我上面说的,你的代码对我来说工作得很好。
您是否将 Interface Builder 中视图的“类标识”更改为 UIView 子类?
(顺便说一句,在 viewDidLoad 中调用 setNeedsDisplay 是不必要的,但也不会造成任何损害。)
Like I said above, your code is working fine for me.
Are you changing the "Class Identity" of the view in Interface Builder to your UIView subclass?
(As an aside, calling setNeedsDisplay in viewDidLoad is unnecessary, but it's also not hurting anything.)
您的图形上下文无效。发生这种情况的原因是:
Your graphics context is invalid. This happens because: