2012-04-19 12 views
7

私はペイントアプリを作成しています。消しゴムツールを実装する方法を知りたいと思います。私は、ユーザーが背景色を変更できるようにしたいので、私の消しゴムツールを白色に塗りつぶしたくない。また、ブラシの硬度を設定することは可能ですか?はいの場合は、どうか教えてください。iOSのペイントアプリ用消しゴムツールを作成する

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

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.view]; 
    UIGraphicsBeginImageContext(self.view.frame.size); 
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *touch = [touches anyObject]; 
CGPoint currentPoint = [touch locationInView:self.view]; 
UIGraphicsBeginImageContext(self.view.frame.size); 
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

lastPoint = currentPoint; 
} 
- (IBAction)clear:(id)sender { 
drawImage.image = nil; 
} 
+0

)..男、それはそれのまわりで私の頭を取得するために私の年齢を取っ=道に沿って他の人を助けこれは興味深い質問ですが、私はあなたがそれをたくさん明確にできると思います。これまでのことを教えてください。おそらくいくつかのコードを含んでいます。 – jtbandes

+0

@jtbandesコード –

+0

を追加しました。このリンクをご覧になり、私の答えをご覧ください。 http://stackoverflow.com/questions/3863931/want-to-add-manual-erasing-option-in-ipad-painting-アプリケーションバイクォーツ/ 12797513#12797513 –

答えて

13

[OK]を、ここで私は、消しゴムツールのためにやったことだ:
私はこのコード行を追加します。
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);

ので、コードはこのようなものなものになりますので

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *touch = [touches anyObject]; 
CGPoint currentPoint = [touch locationInView:self.view]; 
UIGraphicsBeginImageContext(self.view.frame.size); 
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

// I add this 
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear); 

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), sizeSlider.value); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 

CGContextStrokePath(UIGraphicsGetCurrentContext()); 
drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
lastPoint = currentPoint; 
} 
+3

ありがとうございました。それは素晴らしいです。私はこれを非常に長い間探してきました。 –

+0

ありがとうございます:) – iRiziya

4

を私はペイントアプリケーションを作成してもらって、それがiTunesの「簡単落書き」アプリケーションで提供されています:

はあなた

ありがとうここで私がこれまで行ってきたものです。消しゴムのための特別なロジックはありません。背景画像のRGB値を取得し、選択した背景画像に従って

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),R value,G value,B value, 1.0); 

に渡すだけです。

+0

@mat hi ..私たちが描画用カスタムUIViewを作成する場合、私たちは描画メイドなしでペイントできますか? – Hitarth

+0

おそらくあなたはRockに聞きたがっていると思っていましたが、カスタムUIViewを持っていれば、それを修正するたびにビュー自体を描画するためにsetNeedsDiplayを呼び出す必要があります。さもなければ、私の古い[類似の応答](http://stackoverflow.com/a/10667557/251513)を見てください。 – Mat

+1

これは正解ではありません。正しい答えは、より上位の票を持つ以下のものです。 Appleには、コードの1行である消しゴム機能が組み込まれています。この答えは実際には消去されません。イメージの下にあるピクセルと同じ色(背景イメージ)だけをイメージに追加するだけです。 (特に背景画像が変わった場合)、はるかに多くのデータが消えます。 –

1

[OK]を私は一週間の消しゴムを作成する方法の解決策を探していましたが、何回消してもバックグラウンドイメージも消去されてしまったとしても、それを見て変更する必要がありました。

しかし、最終的に私は(私はペイントを消去して、まだ背景のイメージの認識をしていないか、消去している)私に合った解決策を見つけ出し、うまくいけば他人を助けるでしょう。だからここ ...

1を行く)UIViewの 2で2 imageviewsを作成します)あなたはそれが... 3になりたい背景画像との両方imageviewsを設定して)私はかどうかを判断するためにセグメント化されたコントロールを作成しましたユーザーは、あなたが0.0の値でCGContextSetRGBStrokeColorを設定し、そこでの行がある表示されるように、これは重要な原因である

func drawLineForRubber(fromPoint: CGPoint, toPoint: CGPoint) { 

    // 1 
    UIGraphicsBeginImageContext(view.frame.size) 
    let context = UIGraphicsGetCurrentContext() 
    mainImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)) 

    // 2 
    CGContextMoveToPoint(context, fromPoint.x, fromPoint.y) 
    CGContextAddLineToPoint(context, toPoint.x, toPoint.y) 

    // 3 
    CGContextSetBlendMode(context, CGBlendMode.Clear) 
    CGContextSetLineCap(context, CGLineCap.Round) 
    CGContextSetLineWidth(context, 10.0) 
    //this line is very important as it paints the screen clear 
    CGContextSetRGBStrokeColor(context, red, green, blue, 0.0) 
    CGContextSetBlendMode(context, CGBlendMode.Clear) 

    // 4 
    CGContextStrokePath(context) 

    // 5 
    mainImageView.image = UIGraphicsGetImageFromCurrentImageContext() 
    mainImageView.alpha = opacity 
    UIGraphicsEndImageContext() 

} 

..セグメント化されたコントロールが選択されている場合は、以下の機能を追加しました) 4を消去したいですユーザーがペイントを消去しているとき、彼は透明なコロでペイントとともにトップのイメージビューからペイントを消去していますr。しかし、その下に別のイメージビューがあるので、そのイメージは背景イメージに影響を与えずに擦られているように見えます。

それどころか、それは擦られていますが、背後の画像ビューはそれを見せないようにします。 1枚目の画像ビューには塗装した塗料があり、 は2枚目の画像ビューを組み合わせるだけで、背景画像で塗りつぶした画像を書き出すことができます(複数の方法で行うことができますが、 2番目の画像ビューには、元の背景画像があります。

func share() { 

    let layer = UIApplication.sharedApplication().keyWindow!.layer 
    let scale = UIScreen.mainScreen().scale 
    UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale); // reconsider size property for your screenshot 

    layer.renderInContext(UIGraphicsGetCurrentContext()!) 
    let screenshot = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 


    UIGraphicsBeginImageContext(mainImageView.bounds.size) 
    mainImageView.image?.drawInRect(CGRect(x: 0, y: 0, 
     width: mainImageView.frame.size.width, height: mainImageView.frame.size.height)) 

    UIGraphicsEndImageContext() 

    let activity = UIActivityViewController(activityItems: [screenshot], applicationActivities: nil) 
    presentViewController(activity, animated: true, completion: nil) 
} 

希望これは

関連する問題