请设置CG_CONTEXT_SHOW_BACKTRACE环境变量

12

尽管我查看了超过三篇关于这个问题的帖子(发布于2015年),但没有一个解决了我的问题。

每当我运行一个简单的使用UIBezierPath绘制线条的代码时,程序会返回以下内容:

: CGContextSetStrokeColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

: CGContextSetFillColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

: CGContextSetLineWidth: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

: CGContextSetLineJoin: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

: CGContextSetLineCap: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

: CGContextSetMiterLimit: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

: CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

: CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

: CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

@interface line : UIView
UIBezierPath *myPath;
.........
@implementation
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
CGPoint touchLocation = [touch locationInView:self];
myPath = [UIBezierPath bezierPath];
[myPath moveToPoint:touchLocation];
}
- (void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
CGPoint touchLocation = [touch locationInView:self];
[myPath addLineToPoint:touchLocation];
[[UIColor blackColor]setStroke];
[[UIColor greenColor]setFill];
[myPath stroke];
}

如果我使用drawRect进行绘制

- (void) drawRect:(CGRect)rect {
....
}

没有错误弹出。我在想是否之所以收到这些错误是因为touchesBegantouchesMoved不能执行绘制操作?

在cocoa(OS X)中,我曾经使用setNeedsDisplay,但它不存在于cocoa touch中。

我想问的是,有没有办法消除这些错误,或者还有其他方法可以在运行时绘制UIBezierPath

1个回答

23

出现无效上下文错误是因为您尝试在drawRect:方法之外使用绘图操作,因此未设置当前图形上下文。

与在OS X上一样,您应该在drawRect:方法中执行绘图,并使用setNeedsDisplay来更新生成的图像。在iOS上,UIView都提供了setNeedsDisplaysetNeedsDisplayInRect:方法。

因此,结果应该如下所示:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
CGPoint touchLocation = [touch locationInView:self];
myPath = [UIBezierPath bezierPath];
[myPath moveToPoint:touchLocation];
}

- (void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
CGPoint touchLocation = [touch locationInView:self];
[myPath addLineToPoint:touchLocation];
[self setNeedsDisplay];
}

- (void) drawRect:(CGRect)rect {
[[UIColor blackColor]setStroke];
[[UIColor greenColor]setFill];
[myPath stroke];
}

1
我曾经遇到过类似的问题,试图调用drawRect来强制重绘视图加载时绘制的一些图形代码。我试图在drawRect参数中放入自己的值。使用setNeedsDisplay代替解决了我的问题。 - SpaceTrucker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接