我尝试在 ipad 上创建一个简单的手指绘画应用程序。我能够正确地绘制到屏幕的路径,但我希望有一个选项可以完全清除所有绘制路径的屏幕。我目前拥有的代码清除了上下文,但是当我调用绘图代码块时,所有路径都会重新出现在屏幕上。
- (void)drawRect:(CGRect)rect
{
//Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
if(clearContext == 0){
CGContextSetLineWidth(context, 6);
CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
CGContextAddPath(context, drawingPath);
CGContextStrokePath(context);
}
if(clearContext == 1){
//This is the code I currently have to clear the context, it is clearly wrong
//Just used as experimentation.
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextAddPath(context, drawingPath);
CGContextStrokePath(context);
clearContext = 0;
}
}
最佳答案
我假设 drawingPath 是一个 CGPath ...
你在清除 CGPath 吗?这可能是你的问题的原因
在清除路径的操作中,请执行以下操作:
CGPathRelease(drawingPath);
drawingPath = CGPathCreateMutable();
[self setNeedsDisplay];
做你通常做的正常绘图。如果路径为空,则不会绘制任何内容
关于iphone - 如何清除 CGContextRef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12164854/