【发布时间】:2010-08-21 05:21:53
【问题描述】:
我想画一条线。
我写了如下代码。
UIColor *currentColor = [UIColor blackColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
CGContextMoveToPoint(context, startingPoint.x, startingPoint.y);
CGContextAddLineToPoint(context,endingPoint.x , endingPoint.y);
CGContextStrokePath(context);
但这显示异常如下
Sat Aug 21 10:47:20 AAA-rrr-Mac-mini.local Test[2147] <Error>: CGContextSetLineWidth: invalid context 0x0
Sat Aug 21 10:47:20 AAA-rrr-Mac-mini.local Test[2147] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0
Sat Aug 21 10:47:20 AAA-rrr-Mac-mini.local Test[2147] <Error>: CGContextMoveToPoint: invalid context 0x0
Sat Aug 21 10:47:20 AAA-rrr-Mac-mini.local Test[2147] <Error>: CGContextAddLineToPoint: invalid context 0x0
Sat Aug 21 10:47:20 AAA-rrr-Mac-mini.local Test[2147] <Error>: CGContextDrawPath: invalid context 0x0
【问题讨论】:
-
您实际上没有图形上下文。您是在 drawRect: 或其他随机位置执行此操作吗?
-
我在我自己的名为 drawLine:(CGPoint )From:(CGPoint )To 的函数中使用它
-
应该包括任何框架或一些绘图文件?
-
您应该始终使用
drawRect:进行绘图并使用setNeedsDisplay进行任何您想要的刷新。
标签: iphone exception quartz-graphics cgcontext