首页 > 解决方案 > 尝试绘制 touchesBegan 时出现 CGContext 错误

问题描述

- (void)drawRect:(CGRect)rect {

}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGContextRef context = UIGraphicsGetCurrentContext();

    UITouch* touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];

    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);

    CGContextMoveToPoint(context, point.x, point.y);
    CGContextAddLineToPoint(context, point.x + 100, point.y);

    CGContextStrokePath(context);
}

它给了我错误

[未知进程名称] CGContextSetStrokeColorWithColor:无效上下文 0x0。如果要查看回溯,请设置 CG_CONTEXT_SHOW_BACKTRACE 环境变量。[未知进程名称] CGContextMoveToPoint:无效上下文 0x0。如果要查看回溯,请设置 CG_CONTEXT_SHOW_BACKTRACE 环境变量。[未知进程名称] CGContextAddLineToPoint:无效上下文 0x0。如果您想查看回溯,请查看 CG_CONTEXT_SHOW_BACKTRACE 环境变量。[未知进程名称] CGContextDrawPath:无效上下文 0x0。如果要查看回溯,请设置 CG_CONTEXT_SHOW_BACKTRACE 环境变量。

标签: iosobjective-cdrawingcgcontextref

解决方案


推荐阅读