本文介绍了清除iOS中的绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个绘图应用程序,我有一个UIBezeirPath,通过它我可以在touchesMoved中进行绘制,然后将其转换为CGPath,然后再使用CGlayer,

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,

这是我的代码

 -(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 Clear,但是如您所见,当撤消/重做时,我从path获取pathColor并使用blenModeNormal对其进行描边,我看到一条白线,

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,

下面是图像, write-> erase-> undo-> redo

Below is the image,after I write->erase->undo->redo

推荐答案

您是否正确粘贴了代码?如图所示,在 // Erase 语句之前,-touchesMoved:withEvent方法在第14行附近关闭。那意味着您的CGContextRef现在是一个类变量,并且您的for循环永远不会被任何人执行,并且我认为它不会编译,因此我想知道您是否省略了一些代码。

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.

这篇关于清除iOS中的绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 20:11