在iOS中擦除绘图

我正在制作一个绘图应用程序,我有一个UIBezeirPath,用它在touchesMoved中绘制,并将其转换为CGPath,然后绘制到CGlayer,

这是我的代码

-(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); } } 

擦除工作正常,因为我使用blendMode清除,但是当撤消/重做,你可以看到,我得到pathColor从path和笔画与blenModeNormal,我看到一个白线,

下面是图像,我写 – >擦除 – >撤消 – >重做

在这里输入图像说明

你是否正确地粘贴了你的代码? 如上所示,在//Erase语句之前,第14行closures了-touchesMoved:withEvent方法。 这将意味着你的CGContextRef现在是一个类variables,你的for循环从来没有被任何人执行,我不认为它会编译,因此我不知道你是否省略了一些代码。