请设置 CG_CONTEXT_SHOW_BACKTRACE 环境变量
please set CG_CONTEXT_SHOW_BACKTRACE environmental variable
尽管查看了超过 3 个 post(2015 年制作)关于这个问题,但没有解决我的问题。
每当我 运行 时,一个使用 UIBezierPath 画线的简单代码,程序会 return 我这样:
: 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 {
....
}
不会弹出任何错误。我想知道我收到这些错误是不是因为 touchesBegan
和 touchesMoved
无法执行绘图?
在cocoa(OSX)中我曾经用过setNeedsDisplay
,但是在cocoatouch.
中不存在
我想问的是,有没有办法消除这些错误,或者有没有其他方法可以在 运行 时间内绘制 UIBezierPath
。
发生无效上下文错误,因为您正尝试在 drawRect:
方法之外使用绘图操作,因此未设置当前图形上下文。
与OS X 一样,您应该在drawRect:
方法中执行绘图,并使用setNeedsDisplay
更新生成的图像。 setNeedsDisplay
和 setNeedsDisplayInRect:
方法都可用于 iOS 上的 UIView。
所以,结果应该是这样的:
- (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];
}
尽管查看了超过 3 个 post(2015 年制作)关于这个问题,但没有解决我的问题。
每当我 运行 时,一个使用 UIBezierPath 画线的简单代码,程序会 return 我这样:
: 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 {
....
}
不会弹出任何错误。我想知道我收到这些错误是不是因为 touchesBegan
和 touchesMoved
无法执行绘图?
在cocoa(OSX)中我曾经用过setNeedsDisplay
,但是在cocoatouch.
我想问的是,有没有办法消除这些错误,或者有没有其他方法可以在 运行 时间内绘制 UIBezierPath
。
发生无效上下文错误,因为您正尝试在 drawRect:
方法之外使用绘图操作,因此未设置当前图形上下文。
与OS X 一样,您应该在drawRect:
方法中执行绘图,并使用setNeedsDisplay
更新生成的图像。 setNeedsDisplay
和 setNeedsDisplayInRect:
方法都可用于 iOS 上的 UIView。
所以,结果应该是这样的:
- (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];
}