2011-06-07 120 views
0

不是CoreGraphics的專家,我非常感謝您的幫助。從路徑創建蒙版

我試圖創建一個路徑的面具。 (請參閱圖像中的紅色路徑)。 Skitch.jpg

取而代之的是紅色路徑,我希望路徑能夠成爲下方圖像的遮罩。

真的可以用一隻手。在此先感謝

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    mouseSwiped = YES; 

    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.view]; 
    currentPoint.y -= 20; 


    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(), 20.0); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 


    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 

    mouseMoved++; 

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

} 

回答

0

我想出瞭解決的辦法是:

  • 在屏幕上是否有UIView的形象。
  • 與[的UIColor clearColor]背景顏色創建的UIImageView圖像
  • 使用CoreGraphics中填寫的UIImageView顏色
  • 保存的UIImageView,然後在TouchesMoved或UIPanGestureRecognizer,畫savedImage,描邊路徑設置kCGContentBlendClear後,保存圖片和重複。
+0

你能解釋一下嗎?我在這裏完全陷入了同樣的問題 – Soumyajit 2013-10-07 05:39:45

0

一旦你創建的路徑,使用CGContextReplacePathWithStrokedPath將其轉換爲路徑可以填充,以獲得相同的結果輕撫路徑。然後,您可以使用CGContextClip將剪貼蒙版設置爲此路徑。

+0

走到從這走了一會兒。我知道你可能有正確的答案,我只需要更多的細節。 – Jordan 2012-01-21 12:16:43