2009-03-10 4 views
17

사용자가 손가락으로 화면을 그릴 수있는 작은 앱이 있습니다. CGContextRef과 다양한 CG draw 함수를 작성하여 사용자가 그려야하는 UIImageView을 가지고 있습니다. 주로 함수로 획/선을 그립니다. CGContextAddLineToPointiPhone에서 투명한 획을 그릴 때

내 문제는 다음과 같습니다. 사용자가 다양한 색상의 선을 그릴 수 있습니다. 손가락을 사용하여 지금까지 그려진 이미지의 일부분을 삭제하기 위해 "rubber" 도구를 사용할 수있는 능력을 부여하고 싶습니다. 처음에는 뇌졸중에 흰색 색을 사용하여 (CGContextSetRGBStrokeColor 기능으로 설정) 작동했지만 작동하지 않았습니다 ... 나중에 의 UIImage이 실제로 흰색이 아닌 투명한 배경을 가졌다는 것을 발견했기 때문에 ... 그래서 흰 선으로 투명한 이미지로 끝날 것입니다!

스트로크 색을 "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)); 

, 당신의 구형의 시리즈를 추적하고 선 자신을 생성해야 의미 ...

6

나는 ... Bresenham의 라인 알고리즘 (나는 내 ​​자신의 그래픽 루틴을 작성했다 옛날의 일에 다시 맞댄 경청)를 사용하여

- (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); 
     } 
    } 
} 

휴를 종료! 그건 (다소) 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

관련 문제