0

UIKit を使用した単純な描画アプリの作成に関する ray wenderlich のサイトのこの素晴らしいチュートリアルに従っていました。 http://www.raywenderlich.com/18840/how-to-make-a-simple-drawing-app-with-uikit

チュートリアルは素晴らしく、すべてが機能しています。私が抱えている問題は、消しゴム機能の作成に関して、レイが提案した解決策は、背景と同じ色のブラシを使用することでした。私には、これは素晴らしい解決策とは思えません。背景がグラデーションのような無地の色や、多くの塗り絵アプリのような画像ではない場合はどうでしょう。

したがって、基本的な質問は次のとおりです。特定の場所の UIImageView から色を削除する (その領域のすべてのピクセルを透明に変換する) 方法はありますか?

ヘルプやポインタは非常に高く評価されます。ありがとう

4

3 に答える 3

1

この問題の簡単な解決策を見つけた長い検索の後、アプリで同じ問題が発生しました。

私はちょうどタッチメソッドを使用しましたUIViewController

以下は私のアプローチです、

コード:-

   #pragma mark touches stuff...



 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        lastTouch = [touch locationInView:self.editedImageView];
    }

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        currentTouch = [touch locationInView:self.editedImageView];

        CGFloat brushSize;
        if (isEraser) 
        {
            brushSize=eraser;
        }
        else
        {
            brushSize=mark;
        }
        CGColorRef strokeColor = [UIColor whiteColor].CGColor;

        UIGraphicsBeginImageContext(self.editedImageView.frame.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [self.editedImageView.image drawInRect:CGRectMake(0, 0, self.editedImageView.frame.size.width, self.editedImageView.frame.size.height)];
        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetLineWidth(context, brushSize);
        if (isEraser) {

            CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:self.im].CGColor);
        }  
        else
        {
            CGContextSetStrokeColorWithColor(context, strokeColor);
            CGContextSetBlendMode(context, kCGBlendModeClear); 
        }
        CGContextBeginPath(context);
        CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
        CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
        CGContextStrokePath(context);
        self.editedImageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        lastTouch = [touch locationInView:self.editedImageView];
    }

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

    }

入力:

self.editedImageView.image = "Your Custom image";

self.im    =   "Your Custom image";

あなたの問題の簡単な解決策は次のとおりです:-

CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor colorWithPatternImage:self.im].CGColor);

ノート:

これは、画像を再度描画することで消去されません

アップデート:-

 self.im    =   "Your Custom image";

これはこうなる……。

-(void)eraserConfigure
{
    UIImageView *resizeImage=[[[UIImageView alloc]initWithImage:editedImage]autorelease];
    /*UIImage */self.im=[UIImage imageFromView:resizeImage scaledToSize:CGSizeMake(self.editedImageView.frame.size.width, self.editedImageView.frame.size.height)];
}
于 2013-04-03T05:43:24.623 に答える