iOS使用Quartz 2D绘制形状-无效上下文错误
这可能是一件简单的事情。我正试图在ViewDidLoad中绘制我的第一个形状。然而,我的上下文总是空的,我得到“con=0x0”。我不确定我是否正确使用了这个。我已将代码粘贴到下面iOS使用Quartz 2D绘制形状-无效上下文错误,ios,core-graphics,Ios,Core Graphics,这可能是一件简单的事情。我正试图在ViewDidLoad中绘制我的第一个形状。然而,我的上下文总是空的,我得到“con=0x0”。我不确定我是否正确使用了这个。我已将代码粘贴到下面 - (void)viewDidLoad { [super viewDidLoad]; // obtain the current graphics context CGContextRef con = UIGraphicsGetCurrentContext(); // draw
- (void)viewDidLoad
{
[super viewDidLoad];
// obtain the current graphics context
CGContextRef con = UIGraphicsGetCurrentContext();
// draw a black (by default) vertical line, the shaft of the arrow
CGContextMoveToPoint(con, 100, 100);
CGContextAddLineToPoint(con, 100, 19);
CGContextSetLineWidth(con, 20);
CGContextStrokePath(con);
// draw a red triangle, the point of the arrow
CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]);
CGContextMoveToPoint(con, 80, 25);
CGContextAddLineToPoint(con, 100, 0);
CGContextAddLineToPoint(con, 120, 25);
CGContextFillPath(con);
// snip a triangle out of the shaft by drawing in Clear blend mode
CGContextMoveToPoint(con, 90, 101);
CGContextAddLineToPoint(con, 100, 90);
CGContextAddLineToPoint(con, 110, 101);
CGContextSetBlendMode(con, kCGBlendModeClear);
CGContextFillPath(con);
}
调试时,“con”的值为:
代码的问题在于,您试图在不应该绘制的情况下绘制 您可以将绘图代码移动到视图中的
drawRect:
方法,因为操作系统在调用drawRect:
之前准备了一个可以在其中绘制的上下文,或者您可以创建自己的上下文并绘制到其中,将其导出为图像并使用UIImageView显示
con = (CGContext)0x0