2012-02-09 90 views
2

我试图与擦除iPhone应用程序。我遇到了两个问题,如果你有任何一个解决方案,请回答。我想删除一部分图像。iPhone石英绘图橡皮擦

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

2)此外,我想知道,如果有删除的任何其他方式。当擦除速度很快时,它将擦除1/2英寸。有没有办法打通一条路,清理房子?对不起,如果这很难理解。

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

enter image description here

回答

4

此代码应该做你要找的内容:

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

+0

你的答案作品和是伟大的。有没有办法给它添加纹理? http://stackoverflow.com/questions/8793896/quartz-drawing-with-textures – BDGapps 2012-02-10 03:50:13

+0

有iOS中没有公共API像你想的是可以抚摸路径用刷子。 – 2012-02-10 04:18:03

+0

所以如果我想要这个,我将不得不使用openGL ES? – BDGapps 2012-02-10 16:19:49

2

我意识到这是一个老问题,但我想我会扩大埃里克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]; 

} 
+0

好兄弟,当我移动到任何地方我的手指在屏幕上它删除整条生产线。然后,当我再次绘制它绘制我的线和擦除线重新出现。为什么兄弟,为什么? – 2017-01-06 20:44:31

+0

很难说没有看到代码。尝试用你正在使用的代码问一个新问题。如果你想让我看看,请给我一个链接。 – digitalHound 2017-01-07 20:54:54