【问题标题】:iPhone Quartz Drawing EraseriPhone 石英绘图橡皮擦
【发布时间】:2012-03-02 11:06:38
【问题描述】:

我正在尝试制作一个带有擦除功能的 iPhone 应用程序。我遇到了2个问题,如果您有任何一个的解决方案,请回答这个问题。我想删除图像的一部分。

1) 我目前只是清除矩形,但它有一个方形边缘。我希望它是圆形的,我之前尝试过翻译,但这不起作用。我还需要它尽可能少地平移/旋转以保持相同的性能。

2)另外我想知道是否有其他擦除方法。快速擦除时,它会擦除​​ 1/2 英寸。有没有办法划出一条路径并清除矩形或其他东西?对不起,如果这很难理解。

CGRect circleRect = CGRectMake([touch CGPointValue].x, [touch CGPointValue].y, 25, 25);
CGContextClearRect(currentContext,circleRect);

【问题讨论】:

    标签: iphone ios ipad drawing quartz-graphics


    【解决方案1】:

    此代码应该可以满足您的需求:

    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) 清除上下文。

    【讨论】:

    • 您的回答很有效而且很棒。有没有办法为此添加纹理? stackoverflow.com/questions/8793896/…
    • iOS 中没有公共 API 可以使用画笔随意绘制路径。
    • 所以如果我想要这个我就必须使用 openGL ES?
    【解决方案2】:

    我意识到这是一个较老的问题,但我想我会扩展 Eric Reids 的答案,因为我试图在 drawRect 方法之外使用他的示例,并且必须对其进行修改以使其与我在UIView 子类中的“touchesMoved”方法。

    我从 '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];
    
    }
    

    【讨论】:

    • 好的兄弟,当我将手指移动到屏幕上的任意位置时,它会删除整行。然后,当我再次绘制时,它会绘制我的线,并且擦除的线会重新出现。为什么兄弟,为什么?
    • 不看代码很难说。尝试使用您正在使用的代码提出一个新问题。如果你想让我看看,请给我一个链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 2011-03-27
    • 2012-09-30
    • 2011-04-16
    • 2014-05-14
    相关资源
    最近更新 更多