在iPhone中划一条线,例外
我想划一条线 我编写了如下代码在iPhone中划一条线,例外,iphone,exception,quartz-graphics,cgcontext,Iphone,Exception,Quartz Graphics,Cgcontext,我想划一条线 我编写了如下代码 UIColor *currentColor = [UIColor blackColor]; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 2.0); CGContextSetStrokeColorWithColor(context, currentColor.CGColor); CGContextMoveToPoint(context,
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
Sat Aug 21 10:47:20 AAA-rrr-Mac-mini.local测试[2147]:CGContextSetLineWidth:无效上下文0x0
星期六8月21日10:47:20 AAA-rrr-Mac-mini.local测试[2147]:CGContextSetStrokeColorWithColor:无效上下文0x0
8月21日星期六10:47:20 AAA-rrr-Mac-mini.local测试[2147]:CGContextMoveToPoint:无效上下文0x0
8月21日星期六10:47:20 AAA-rrr-Mac-mini.local测试[2147]:CGContextAddLineToPoint:无效上下文0x0
星期六8月21日10:47:20 AAA-rrr-Mac-mini.local测试[2147]:CGContextDrawPath:无效上下文0x0
您没有有效的图形上下文UIGraphicsGetCurrentContext()
显然返回了nil
你想去哪里画画
如果要绘制到屏幕,则应实现UIView
的drawRect:
方法或其子类之一,并让iOS调用该方法(通过触发部分屏幕的刷新)。然后,在执行drawRect:
期间,您将拥有一个有效的图形上下文
如果要绘制屏幕外像素贴图,则必须使用UIGraphicsBeginImageContext
或类似功能自己创建图形上下文
编辑:
因此,要将图形绘制到UIView中,需要创建UIView子类并重写drawRect:
然后打开XIB文件(可能已经在Interface Builder中有了),在那里添加UIView并选择MyUIView作为类(Inspector窗口最后一个选项卡上的第一个字段)。您没有有效的图形上下文。
UIGraphicsGetCurrentContext()
显然返回了nil
你想去哪里画画
如果要绘制到屏幕,则应实现UIView
的drawRect:
方法或其子类之一,并让iOS调用该方法(通过触发部分屏幕的刷新)。然后在执行drawRect:
期间,您将拥有有效的图形上下文
如果要绘制屏幕外像素贴图,则必须使用UIGraphicsBeginImageContext
或类似功能自己创建图形上下文
编辑:
因此,要将图形绘制到UIView中,需要创建UIView子类并重写drawRect:
然后,打开XIB文件(您可能已经在Interface Builder中拥有了它),在那里添加UIView并选择MyUIView作为类(Inspector窗口最后一个选项卡上的第一个字段)。在调用
CGContextMoveToPoint
之前,您还需要调用CGContextBeginPath(ctx);
- (void) drawRect: (CGRect) rect
{
UIColor *currentColor = [UIColor blackColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
CGContextBeginPath(context); // <---- this
CGContextMoveToPoint(context, self.bounds.origin.x, self.bounds.origin.y);
CGContextAddLineToPoint(context, self.bounds.origin.x + self.bounds.size.x, self.bounds.origin.y + self.bounds.size.y);
CGContextStrokePath(context);
}
-(void)drawRect:(CGRect)rect
{
UIColor*currentColor=[UIColor blackColor];
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(上下文,2.0);
CGContextSetStrokeColorWithColor(上下文,currentColor.CGColor);
CGContextBeginPath(context);//在调用CGContextMoveToPoint
- (void) drawRect: (CGRect) rect
{
UIColor *currentColor = [UIColor blackColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
CGContextBeginPath(context); // <---- this
CGContextMoveToPoint(context, self.bounds.origin.x, self.bounds.origin.y);
CGContextAddLineToPoint(context, self.bounds.origin.x + self.bounds.size.x, self.bounds.origin.y + self.bounds.size.y);
CGContextStrokePath(context);
}
-(void)drawRect:(CGRect)rect
{
UIColor*currentColor=[UIColor blackColor];
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(上下文,2.0);
CGContextSetStrokeColorWithColor(上下文,currentColor.CGColor);
CGContextBeginPath(context);//您实际上没有图形上下文。您是在drawRect:或其他任意位置中执行此操作的吗?我在自己名为drawLine:(CGPoint)From:(CGPoint)的函数中使用它Toshould包括任何用于绘图的框架或文件?您应该始终使用drawRect:
进行绘图,并使用setNeedsDisplay
进行任何刷新。您实际上没有图形上下文。您是在drawRect:或其他任意位置中进行此操作的吗?我在自己名为drawLine:(CGPoint)的函数中使用它,来自:(CGPoint)Toshould include any framework or some file for drawing?你应该始终使用drawRect:
进行绘制,并使用setNeedsDisplay
进行任何刷新。你能举个小例子吗?如果你回答你想在哪里绘制,我可以举个例子。我想在UIView中画一条线。你能举个小例子吗?我可以给你一个例子示例如果您回答要绘制到的位置。我想在UIView中绘制一条线,您还需要使用CGContextClosePath
您还需要使用CGContextClosePath