1

UIImage に (ブラシ アプリケーションのように) いくつかの線を描画したら、ゴムのようにそれらをクリアします。このサイトやGoogleでいろいろ検索して、コードをいくつか試してみましたが、正解が見つかりませんでした。ここに私が書くために使用するコードがあります:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    if(isDrawing == YES) {

        UITouch *touch = [touches anyObject];
        CGPoint currentPoint = [touch locationInView:mainImageView.superview];

        UIGraphicsBeginImageContext(mainImageView.frame.size);
        [mainImageView.image drawInRect:CGRectMake(0, 0, mainImageView.frame.size.width, mainImageView.frame.size.height)];

        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), dimension);

        const CGFloat *components = CGColorGetComponents([color CGColor]);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0], components[1], components[2], components[3]);

        CGContextBeginPath(UIGraphicsGetCurrentContext());
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());

        mainImageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        lastPoint = currentPoint;

    } else {
        //Clear current line (like a rubber would do)
    }
}
4

3 に答える 3

2

このシナリオ[UIColor clearColor]では機能しませんCGContextClearRect(context,rect);。領域をクリアするために使用してください!

于 2013-01-25T14:57:31.450 に答える
0

コンテキストの必要な部分を で埋めます[UIColor clearColor]

于 2013-01-25T14:34:43.600 に答える
0

正しいアプローチは、次のようなベジエ パスを使用することです。

  • すべての描画を記録します (ただし、実際には描画しません.. DrawRect では、「ストローク」を使用して描画します)
  • 厚みや色など…
  • ビューに効果的に描画します

それをクリアして (「removeAllPoints」を使用)、setNeedsDisplay を呼び出すと、背景が再表示されます。

別のツールを使用して書いたパスをラバークリアしたい場合 (たとえば、鉛筆ツールを使用して青色で書き込み、ラバーツールを使用して青色の線をクリアし、背景を再表示します)、「containsPoint:」を使用できます。ベジェパスからいくつかのポイントを削除します。

私たちはそのようにしました。

ベジエパスの使用は簡単ではありませんが、非常にエキサイティングです.. :)

于 2013-08-03T11:50:26.523 に答える