gpt4 book ai didi

objective-c - 请设置 CG_CONTEXT_SHOW_BACKTRACE 环境变量

转载 作者:太空狗 更新时间:2023-10-30 03:31:14 30 4
gpt4 key购买 nike

尽管查看了超过 3 个关于此问题的帖子(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

最佳答案

发生无效上下文错误,因为您正尝试在 drawRect: 方法之外使用绘图操作,因此没有设置当前图形上下文。

与在 OS X 上一样,您应该在 drawRect: 方法中执行绘图,并使用 setNeedsDisplay 更新生成的图像。 setNeedsDisplaysetNeedsDisplayInRect: 方法都可用于 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];
}

关于objective-c - 请设置 CG_CONTEXT_SHOW_BACKTRACE 环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34764459/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com