2012-02-29 53 views
7

我正在做一些使用CGContext的繪圖。我使用PNG文件,這樣目前掩蓋圖紙:掩蓋CGContext與CGPathRef?

UIImage * myImage = [UIImage imageNamed:@"frame.png"]; 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextClipToMask(context, self.view.bounds, myImage.CGImage); 

這工作得很好,但現在我想使用現有CGPathRef作爲我的面具。 我應該看看如何將CGPathRef轉換爲UIImage和掩碼? 如果是的話我將如何去做轉換? - 或 - 有沒有更好的方法來解決這個問題?

回答

10
CGContextAddPath(context, yourPath); 
CGContextClip(context); 
+1

這對rects&ellipse很有效。但是,如果路徑是由addArcWithCenter創建的,則不適用於我:radius:...並且當我只需要一條像素線時。 – 2015-06-21 20:28:07

+0

如果我們不希望它被剪輯,我們應該怎麼做?例如,如果我們想要遮罩圖像上的自定義路徑而不將其剪切到路徑的邊界? @cxa – 2018-02-04 21:19:36