2009-03-10 20 views
17

私は、ユーザーが指で画面に描くことを可能にする小さなアプリを持っています。 CGContextRefとさまざまなCG draw関数を作成して、ユーザーが描画するUIImageViewを持っています。私は主に関数でストローク/ラインを描きます。CGContextAddLineToPointiPhoneで透明な線を描く方法(または画像の透明部分を描く方法)

今私の問題は次のとおりです。 ユーザーはさまざまな色の線を描くことができます。私は彼に"rubber"ツールを使用して、これまでに描画された画像の一部を指で削除する機能を与えたいと考えています。私は当初、ストロークに白い色を使用してこれを行いましたが(CGContextSetRGBStrokeColor機能で設定されていましたが)、うまくいきませんでした...後で、UIImageViewUIImageが実際に透明な背景だから私はそれに白い線で透明なイメージで終わるだろう!

ストロークカラーを"transparent"に設定する方法はありますか?CGContextRefのコンテンツをユーザーの指で動かすときにそのコンテンツを消去する方法はありますか? おかげ

答えて

57

...(X1、Y1、X2、および適切な値とY2を置き換え):

CGContextSetBlendMode(context, kCGBlendModeClear) 
+1

は、これが描かれたラインと一緒に画像を消去しませんか?画像の消去を防止したい場合はどうすればよいですか?塗料だけを消す? – Shailesh

-13
UIColor *clearColor = [UIColor clearColor]; 
1

は、私が使用して試してみた:

CGContextSetStrokeColorWithColor (myContext, [[UIColor clearColor] CGColor]); 

が、それは動作しません、それは目に見えない色(目に見えない色で文脈の上に「描画」しているように見えるので、 +それが描いている色=それが描画されている色)。

(最適ではない)私が見つけた唯一の解決策は、次のとおりです。残念ながら

CGContextClearRect (myContext, CGRectMake(x, y, width, height)); 

、あなたはrectsのシリーズをトレースし、ラインを自分で生成する必要がありますつまり...

6

私は

- (void) contextEraseLine:(CGContextRef) ctx from:(CGPoint)startPoint to:(CGPoint) endPoint withThickness:(int)thickness { 
    int x, cx, deltax, xstep, 
    y, cy, deltay, ystep, 
    error, st, dupe; 

    int x0, y0, x1, y1; 

    x0 = startPoint.x; 
    y0 = startPoint.y; 
    x1 = endPoint.x; 
    y1 = endPoint.y; 

    // find largest delta for pixel steps 
    st = (abs(y1 - y0) > abs(x1 - x0)); 

    // if deltay > deltax then swap x,y 
    if (st) { 
     (x0 ^= y0); (y0 ^= x0); (x0 ^= y0); // swap(x0, y0); 
     (x1 ^= y1); (y1 ^= x1); (x1 ^= y1); // swap(x1, y1); 
    } 

    deltax = abs(x1 - x0); 
    deltay = abs(y1 - y0); 
    error = (deltax/2); 
    y = y0; 

    if (x0 > x1) { xstep = -1; } 
    else   { xstep = 1; } 

    if (y0 > y1) { ystep = -1; } 
    else   { ystep = 1; } 

    for ((x = x0); (x != (x1 + xstep)); (x += xstep)) 
    { 
     (cx = x); (cy = y); // copy of x, copy of y 

     // if x,y swapped above, swap them back now 
     if (st) { (cx ^= cy); (cy ^= cx); (cx ^= cy); } 

     (dupe = 0); // initialize no dupe 

     if(!dupe) { // if not a dupe, write it out 
      //NSLog(@"(%2d, %2d)", cx, cy); 

      CGContextClearRect(ctx, CGRectMake(cx, cy, thickness, thickness)); 

    } 

     (error -= deltay); // converge toward end of line 

     if (error < 0) { // not done yet 
      (y += ystep); 
      (error += deltax); 
     } 
    } 
} 

ふう...ブレゼンハムのアルゴリズム(私は自分のグラフィックルーチンを記述しなければならなかった昔の時代に戻ってharkening)を使用して終了しました!それは(幾分)clunkyな消しゴムのラインを作成するために行くための長い道のりです。

- (void)eraseStart { 
    // erase lines 
    UIGraphicsBeginImageContext(drawingBoard.size); 
    ctx = UIGraphicsGetCurrentContext(); 
    CGContextDrawImage(ctx,CGRectMake(0,0,drawingBoard.size.width, drawingBoard.size.height),[drawingBoard CGImage]); 
} 

- (void)eraseEnd { 
    drawingBoard = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    [drawingView removeFromSuperview]; 
    [drawingView release]; 

    drawingView = [[UIImageView alloc] initWithImage:drawingBoard]; 
    drawingView.frame = CGRectMake(intEtchX, intEtchY, intEtchWidth, intEtchHeight); 

    [self.view addSubview:drawingView]; 
} 

これは、あなたがすでにdrawingView(UIImageView)とdrawingBoard(UIImage)を作成している前提としています。それを使用する

は、ような何かを行います。

その後、ラインを消去するために、単にような何か:

CGContextRef ctx = UIGraphicsGetCurrentContext(); 
[self eraseStart]; 
[self contextEraseLine:ctx from:CGPointMake (x1, y1) to:CGPointMake (x2, y2) withThickness:10]; 
[self eraseEnd]; 

これは、トリックを行います

-2
//erase part 
if(mouseSwiped) 
{ 

//**************Working Code*************// 


UIGraphicsBeginImageContext(frontImage.frame.size); 
[frontImage.image drawInRect:CGRectMake(0, 0, frontImage.frame.size.width, frontImage.frame.size.height)]; 
CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGImageAlphaNone); //kCGImageAlphaPremultipliedLast); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10); 
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1, 0, 0, 10); 
CGContextBeginPath(UIGraphicsGetCurrentContext()); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(lastPoint.x, lastPoint.y, 10, 10)); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 
frontImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 


lastPoint = currentPoint; 

mouseMoved++; 

if (mouseMoved == 10) 
{ 
    mouseMoved = 0; 
} 
} 
+0

これは機能しません - 画像ビューの上に茶色の線が引かれます – Shailesh

関連する問題