2012-02-09 8 views
2

私はiPhoneアプリケーションを消去しようとしています。私は2つの問題に遭遇しています。いずれかの解決策がある場合は、これに答えてください。私はイメージの一部を消したいです。iPhone Quartz Drawing Eraser

1)現在、矩形を消去していますが、四角形のエッジがあります。私はそれが丸くなるようにしたい、私は以前に翻訳しようとしたが、これは動作しません。同じパフォーマンスを維持するために、できるだけ数回翻訳/回転する必要があります。

2)さらに、他に消去方法があるかどうかを知りたかったのです。速く消去すると1/2インチも消去されます。パスを鳴らし、rectや何かをクリアする方法はありますか?これが分かりにくいのであれば申し訳ありません。

ラウンドにそれを作る

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)、:

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

enter image description here

答えて

4

このコードは、あなたが探しているものを行う必要があります2)CGContextSetBlendMode(context, kCGBlendModeClear)は、コンテキストをクリアします。

+0

あなたの答えは動作し、素晴らしいです。これにテクスチャを追加する方法はありますか? http://stackoverflow.com/questions/8793896/quartz-drawing-with-textures – BDGapps

+0

iOSにパブリックAPIはありません。これは、希望通りにブラシを使用してパスをストロークさせることができます。 –

+0

もし私がこれを望むのであれば、私はopenGL ESを使わなければならないでしょうか? – BDGapps

2

これはもっと古い質問ですが、私は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

私は画面上のどこにでも指を動かすと、行全体が削除されます。それから私が再び描くときに私のラインが引かれ、消去されたラインが再び現れる。なぜ仲間、なぜ? –

+0

コードを見ずに言うのは難しいです。あなたが使っているコードで新しい質問をしてみてください。あなたが私に見てもらいたいなら、私にリンクを張りなさい。 – digitalHound