问题
I am working on a drawing app, I have a UIBezeirPath, with which I draw in touchesMoved, and convert it to CGPath and then draw on to CGlayer,
Here is my code
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
self.currentPath = [[DrawingPath alloc] init];
if(m_eraseButtonClicked)
{
[self.currentPath setPathColor:[UIColor backgroundColor]];
}
else
{
[self.currentPath setPathColor:self.lineColor];
}
CGPathRef cgPath = self.currentPath.path.CGPath;
mutablePath = CGPathCreateMutableCopy(cgPath);
}
- (void)Erase
{
CGContextRef layerContext = CGLayerGetContext(self.DrawingLayer);
CGContextSetBlendMode(layerContext,kCGBlendModeClear);
CGContextSetLineWidth(layerContext, self.eraseWidth);
CGContextBeginPath(layerContext);
CGContextAddPath(layerContext, mutablePath);
CGContextStrokePath(layerContext);
}
- (void)UndoRedoFunction
{
//Undo/Redo
for(int i =0; i<[undoArray count];i++)
{
DrawingPath *drawPath = [undoArray objectAtIndex:i];
CGPathRef path = drawPath.path.CGPath;
mutablePath = CGPathCreateMutableCopy(path);
CGContextBeginPath(layerContext);
CGContextAddPath(layerContext, mutablePath);
CGContextSetLineWidth(layerContext, drawPath.pathWidth.floatValue);
CGContextSetStrokeColorWithColor(layerContext, drawPath.pathColor.CGColor);
CGContextSetFillColorWithColor(layerContext, drawPath.pathColor.CGColor);
CGContextSetBlendMode(layerContext,kCGBlendModeNormal);
CGContextStrokePath(layerContext);
CGPathRelease(mutablePath);
}
}
Erase works fine, because I use blendMode Clear, but when undo/Redo, as you can see, I get the pathColor from path and stroke it with blenModeNormal, I see a white line,
Below is the image,after I write->erase->undo->redo
回答1:
Did you paste in your code correctly? As it's shown, the -touchesMoved:withEvent method is closed out around line 14, just before the //Erase
statement. That would mean that your CGContextRef is now a class variable, and your for loop is never executed by anyone, and I don't think it would compile, hence I wonder if you are omitting some code.
来源:https://stackoverflow.com/questions/22033228/eraser-the-drawing-in-ios