iPhone石英绘画橡皮擦

3
我正在尝试制作一个带有擦除功能的iPhone应用程序。我遇到了两个问题,如果您有任何解决方案,请回答本文。我想要擦除图像的一部分。
1)我目前只是清除矩形,但它具有正方形的边缘。我希望它是圆形的,我以前尝试过转换但这不起作用。我还需要尽可能少地旋转来保持相同的性能。
2)此外,我想知道是否有其他擦除方式。当快速擦除时,每半英寸就会出现擦除。是否有一种方法可以笔画路径并清除矩形或其他什么东西?如果难以理解,敬请谅解。
CGRect circleRect = CGRectMake([touch CGPointValue].x, [touch CGPointValue].y, 25, 25);
CGContextClearRect(currentContext,circleRect);

enter image description here

2个回答

5
这段代码应该可以满足你的需求:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.strokeWidth);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextSetStrokeColorWithColor(context, [[UIColor clearColor] CGColor]);
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextStrokePath(context);     
CGContextFlush(context);

重点是:
1)CGContextSetLineCap(context, kCGLineCapRound),使它变成圆形。
2)CGContextSetBlendMode(context, kCGBlendModeClear),清除上下文。

你的答案可行且很棒。有没有办法给它添加纹理?http://stackoverflow.com/questions/8793896/quartz-drawing-with-textures - BDGapps
iOS中没有公共API可以像您想要的那样使用画笔描绘路径。 - rob mayoff
那么如果我想要这个,我必须使用OpenGL ES吗? - BDGapps

2

我知道这是一个比较老的问题,但是我认为我可以扩展Eric Reid的答案,因为我正在尝试在drawRect方法之外使用他的示例,并且必须进行修改以使其适用于我在UIView子类中创建的上下文。

我从'touchesMoved'调用此方法,并将其传递到我正在绘制的视图中触摸的CGPoint。

-(void) processEraseAtPoint:(CGPoint)point
{
    // setup a context with the size of our canvas view (the canvas view is the UIView instance I'm drawing into)
    UIGraphicsBeginImageContext(self.canvasView.bounds.size);

    // get a reference to the context we just created
    CGContextRef context = UIGraphicsGetCurrentContext();

    // draw the image we want to edit into this context (this is the image containing the drawing I want to erase part of)
    [self.canvasView.incrementalImage drawAtPoint:CGPointZero];

    // set our context options
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, self.canvasView.settings.brushDiameter);
    CGContextSetBlendMode(context, kCGBlendModeClear);

    // make the color clear since we're erasing
    CGContextSetStrokeColorWithColor(context, [[UIColor clearColor] CGColor]);

    // start our path in this context
    CGContextBeginPath(context);

    // set our first point
    CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);

    // draw from our last point to this point
    CGContextAddLineToPoint(context, point.x, point.y);

    // stroke this path (in this case it's clear so it will erase what's there)
    CGContextStrokePath(context);

    // set our incrementalImage in the canvasView with the updated image from this context
    // Note that in the canvasView 'drawRect' method I am calling 
    // '[self.incrementalImage drawInRect:rect]', so this new image will get drawn 
    // in my canvasView when I call 'setNeedsDisplay'
    self.canvasView.incrementalImage = UIGraphicsGetImageFromCurrentImageContext();

    // cleanup our context
    CGContextFlush(context);
    UIGraphicsEndImageContext();

    // set our last touch point for the next line segment
    lastTouch = point;

    // update our view
    [self.canvasView setNeedsDisplay];

}

好的兄弟,当我在屏幕上移动手指时,它会删除整行。然后当我再次绘制时,它会画出我的线条并且被擦除的线条重新出现。为什么啊,兄弟? - user3674231
很难在没有看到代码的情况下做出判断。请尝试使用您正在使用的代码提出一个新问题。如果您希望我来查看,请通过链接与我联系。 - digitalHound

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接