Eraser the drawing in iOS

我们两清 提交于 2019-12-20 07:13:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!