iPhone石英绘图橡皮擦

时间:2012-02-09 22:38:41

标签: iphone ios ipad drawing quartz-graphics

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

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

2)此外,我想知道是否还有其他的擦除方法。当快速擦除时,它会擦掉1/2英寸。有没有办法抚摸一条路径并清除矩形或什么东西?对不起,如果这很难理解。

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

enter image description here

2 个答案:

答案 0 :(得分: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)清除上下文。

答案 1 :(得分:2)

我意识到这是一个较老的问题,但我想我会扩展Eric Reids的答案,因为我试图在drawRect方法之外使用他的例子,并且必须修改它以使其与我在a中创建的上下文一起工作' touchesMoved' UIView子类中的方法。

我将这种方法称为“触动移动”'并在我吸引的视图中传递触摸的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];

}