2014-04-18 20 views
0

我正在使用UIBezierPath在我的圖像中使用此剪切一個洞。ios - 如何在同一視圖中使用兩條貝塞爾路徑?

UIBezierPath *currentPath = [UIBezierPath bezierPath]; 

    CGPoint tempPoint = CGPointMake(leftEyePos.x+30,leftEyePos.y-25); 
    [currentPath moveToPoint:CGPointMake(tempPoint.x, tempPoint.y)]; 
    tempPoint = CGPointMake(leftEyePos.x-40,leftEyePos.y-25); 
    [currentPath addLineToPoint:CGPointMake(tempPoint.x, tempPoint.y)]; 
    tempPoint = CGPointMake(leftEyePos.x-40,leftEyePos.y+10); 
    [currentPath addLineToPoint:CGPointMake(tempPoint.x, tempPoint.y)]; 
    tempPoint = CGPointMake(leftEyePos.x+30,leftEyePos.y+10); 
    [currentPath addLineToPoint:CGPointMake(tempPoint.x, tempPoint.y)]; 
    tempPoint = CGPointMake(leftEyePos.x+30, leftEyePos.y-25); 
    [currentPath addLineToPoint:CGPointMake(tempPoint.x, tempPoint.y)]; 

// Create an image context containing the original UIImage. 
    UIGraphicsBeginImageContext(filteredImage.size); 
    [filteredImage drawAtPoint:CGPointZero]; 

    // Clip to the bezier path and clear that portion of the image. 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextAddPath(context,currentPath.CGPath); 
    CGContextAddPath(context,nextPath.CGPath); 
    CGContextClip(context); 
    CGContextClearRect(context,CGRectMake(0,0,filteredImage.size.width,filteredImage.size.height)); 

    // Build a new UIImage from the image context. 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

我想是用另一種路徑。我怎樣才能做到這一點?我想這樣創造新UIBezierPath:UIBezierPath *newPath = [UIBezierPath bezierPath];我又添加CGPoint:

CGPoint mouthTempPoint = CGPointMake(mouthPos.x-30, mouthPos.y-10); 

    [newPath addLineToPoint:CGPointMake(mouthTempPoint.x, mouthTempPoint.y)]; 
    mouthTempPoint = CGPointMake(mouthPos.x+30, mouthPos.y-10); 
    [newPath addLineToPoint:CGPointMake(mouthTempPoint.x, mouthTempPoint.y)]; 
    mouthTempPoint = CGPointMake(mouthPos.x+30, mouthPos.y-30); 
    [newPath addLineToPoint:CGPointMake(mouthTempPoint.x, mouthTempPoint.y)]; 
    mouthTempPoint = CGPointMake(mouthPos.x-30, mouthPos.y-30); 
    [newPath addLineToPoint:CGPointMake(mouthTempPoint.x, mouthTempPoint.y)]; 
    mouthTempPoint = CGPointMake(mouthPos.x-30, mouthPos.y-10); 
    [newPath addLineToPoint:CGPointMake(mouthTempPoint.x, mouthTempPoint.y)]; 

但是我得到一個錯誤。我的錯誤是<Error>: void CGPathAddLineToPoint(CGMutablePathRef, const CGAffineTransform *, CGFloat, CGFloat): no current point.

有人可以幫助我嗎?

+1

你沒有開始'moveToPoint'在第二個例子嗎? –

+0

我只是複製另一個貝塞爾路徑和cg點。我只是改變變量名稱。我在這裏錯過了什麼? – user3548852

回答

1

看看你的第一代碼部分啓動路徑

CGPoint tempPoint = CGPointMake(leftEyePos.x+30,leftEyePos.y-25); 
    [currentPath moveToPoint:CGPointMake(tempPoint.x, tempPoint.y)]; 

開始新行,你必須有一個起點

+0

這是我的第二條貝塞爾路徑的起點'CGPoint mouthTempPoint = CGPointMake(mouthPos.x-30,mouthPos.y-10);'。我在這裏錯過了什麼,或者我不能在一個視圖中使用兩條貝塞爾路徑? – user3548852

+0

貝塞爾路徑的視圖沒有限制,關於你的起點是你創建它,但你在哪裏調用'moveToPoint:'它? – sage444

+0

我檢查UIBezier參考,並看到moveToPoint:方法。這是我正在尋找。謝謝。 – user3548852