2012-04-19 3 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- application-by-quartz/12797513 # 12797513 –

답변

13

내가 지우개 도구를 무슨 짓을했는지 :

- (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

나는 페인트 응용 프로그램을 가지고 있고 그것은 아이튠즈 "쉬운 낙서"응용 프로그램에서 사용할 수 있습니다. 지우개를위한 특별한 논리는 없습니다. 배경 이미지의 RGB 값을 가져와 선택한 배경 이미지에 따라

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

으로 전달하면됩니다. 좋아 여기

+0

@mat 안녕하세요 .. 우리가 draw UI 대신 페인트 할 수있는 것보다 페인트 용 사용자 정의 UIView를 만드는 경우? – Hitarth

+0

아마 Rock을 물어보고 싶었지만, 그렇게 생각하지는 않습니다. 사용자 정의 UIView가있는 경우 수정할 때마다 뷰 자체를 그리기 위해 setNeedsDiplay를 호출해야합니다. 그렇지 않으면, 나의 오래된 [비슷한 응답] (http://stackoverflow.com/a/10667557/251513)을 살펴보십시오. – Mat

+1

이것은 정답이 아닙니다. 정답은 아래에있는 투표 중 하나입니다. Apple은 한 줄의 코드 인 지우개 기능을 내장하고 있습니다. 이 대답은 실제로 지워지지도 않습니다. 단순히 이미지에 "페인트"를 추가하는 것입니다. 이미지 아래에있는 픽셀과 동일한 색상 (배경 이미지) 만 추가하면됩니다. 실제로 지우지는 않습니다 (특히 배경 이미지가 변경된 경우). 그리고 훨씬 더 많은 데이터가 삭제됩니다. –

1

좋아 너무 :
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);

그래서 코드가 뭔가 이런 식으로 뭔가있을 것이다 :
나는이 코드 줄을 추가 나는 일주일 동안 지우개를 만드는 방법에 대한 해결책을 찾고 있었고, 내가 그것을 보았던 방식을 바꿔야 만 아무리 많은 것을 지우려고해도 배경 이미지를 지울 수 있었다.

그러나 결국 나는 (페인트를 지울 수 있고 여전히 배경 이미지에 대한 인식을 그대로 유지하거나 지워 버릴 수있는) 저에게 적합한 솔루션을 찾았고 다른 사람들에게 도움이되기를 바랍니다. 그래서 여기 3) 나는 여부를 결정하기 위해 세그먼트 컨트롤을 만든 ...

1) 당신이 원하는 배경 이미지를 모두 imageviews을 설정 UIView의 2) 2 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) 다음 함수가 추가 소거 원이 중요 원인 사용자가 페인트를 지울 때, 그는 transperent colo로 페인트와 함께 상단 이미지 뷰에서 페인트를 지우고있다. 아르 자형. 그러나 그 밑에 또 다른 이미지 뷰가 있기 때문에 배경 이미지에 영향을 미치지 않으면 서 문지른 것처럼 보입니다.

반대로 그것은 뒤죽박죽이지만 이미지 뷰는 뒤쳐져 보이지 않습니다. 첫 번째 이미지 뷰에는 페인트 한 페인트가 있고 은 이미지 뷰를 결합한 것입니다 (두 가지 방법으로 할 수 있지만 다음과 같이 할 수 있습니다). 두 번째 이미지 뷰에는 원하는 원래 배경 이미지가 있습니다.

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

희망이 그것의 주위에 내 머리 =)