【问题标题】:Draw a line in iPhone , Exception在 iPhone 中画一条线,异常
【发布时间】: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


【解决方案1】:

您还需要在拨打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);
}

【讨论】:

  • 你还需要使用CGContextClosePath
【解决方案2】:

您没有有效的图形上下文。 UIGraphicsGetCurrentContext() 显然返回了nil。

你想画到哪里?

如果你想在屏幕上绘图,那么你应该实现UIView 或其子类之一的drawRect: 方法,并让iOS 调用该方法(通过触发部分屏幕的刷新)。那么在drawRect:的执行过程中你就会有一个有效的图形上下文。

如果要绘制到屏幕外像素图,则必须使用UIGraphicsBeginImageContext 或类似函数自己创建图形上下文。

编辑:

所以要绘制到 UIView,你需要创建一个 UIView 子类并覆盖 drawRect:

@interfaceMyView : UIView {
}

@end


@implementation MyUIView

- (void) drawRect: (CGRect) rect
{
  UIColor *currentColor = [UIColor blackColor];

  CGContextRef context = UIGraphicsGetCurrentContext(); 
  CGContextSetLineWidth(context, 2.0);
  CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
  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);
}

然后您打开 XIB 文件(您可能已经在 Interface Builder 中添加了一个 UIView 并选择 MyUIView 作为类(Inspector 窗口中最后一个选项卡上的第一个字段)。

【讨论】:

  • 你能举个小例子吗?
  • 如果你回答你想画到哪里,我可以给你一个例子。
  • 我想在UIView中画一条线
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-12
相关资源
最近更新 更多