2013-04-02 76 views
0

我在ray wenderlich的網站上關於用UIKit創建一個簡單的繪圖應用程序的這個偉大的教程。 http://www.raywenderlich.com/18840/how-to-make-a-simple-drawing-app-with-uikit實現繪圖應用的橡皮擦功能

該教程是偉大的,一切工作。我遇到的問題是,在創建橡皮擦功能時,ray提出的解決方案是使用與背景具有相同顏色的筆刷。對我來說,這似乎不是一個很好的解決方案。如果背景不像純色那樣像漸變色或任何圖像,就像在很多着色書應用程序中一樣。

所以基本上問題是:是否有一種方法可以在給定的位置從UIImageView中移除顏色(將該區域中的所有像素轉換爲透明)。

任何幫助或指針將大大appriciated。由於

+1

您是否嘗試過使用clearColor? – MXV

+0

發現此:http://stackoverflow.com/questions/629409/how-to-draw-a-transparent-stroke-or-anyway-clear-part-of-an-image-on-the-iphon 但不知道如何實現這...任何幫助? –

回答

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

注:

這不是的unerase再次繪製的圖像,在

更新: -

self.im = "Your Custom image"; 

this會是這樣的....

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

注意:如果您的背景是白色或其他顏色,則不會擦除但重新繪製,如果您的背景是相同的,則使用該顏色或圖像意味着使用上述代碼 – Spynet

+0

真棒......必須與代碼位..我的是有點太亂了,但終於得到它的工作..謝謝 –

+0

@UmerHassam謝謝並面臨任何問題讓我知道 – Spynet

0

用畫筆但是對於顏色的使用:

[UIColor clearColor] 
+0

如何將[UIColor清晰顏色]傳遞給CGContextSetRGBStrokeColor()? –

+0

嘗試使用CGContextSetStrokeColorWithColor代替。 CGContextSetStrokeColorWithColor(上下文,[UIColor clearColor]。CGColor) –

1

我可以解決使用下面的代碼的問題:

CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear); 

Reference