2012-04-06 64 views
0

我有一個JPG圖片,在一個矩形,圓形物體,並希望使圓形物體透明的envoirement ...的iOS:逆UIBezierPath(bezierPathWithOvalInRect)

Example

(在刪除紅色區域這個例子)

在這個iOS make part of an UIImage transparent和「UIBezierPath bezierPathWithOvalInRect」的幫助下,我已經取得了一點成功,但是這在我的對象周圍形成了一條路徑,我需要反轉它,使外部而不是內部的透明路徑..

我不是舒爾,我不得不改變我的代碼,以獲得正確的結果..

這裏是我的代碼:

//BezierPath 
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)]; 

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

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

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

任何人有分辨?

+0

你只想讓紅色變得透明或黑變透明? – 2012-04-06 10:27:37

回答

9

要只黑色圓圈內畫出你應該CGContextClip(context);後移至[_imgTemp.image drawAtPoint:CGPointZero];來,然後徹底刪除CGContextClearRect(...)電話:

//BezierPath 
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)]; 

// Create an image context containing the original UIImage. 
UIGraphicsBeginImageContext(_imgTemp.image.size); 

// Clip to the bezier path and clear that portion of the image. 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextAddPath(context,bezierPath.CGPath); 
CGContextClip(context); 

// Draw here when the context is clipped 
[_imgTemp.image drawAtPoint:CGPointZero]; 

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

嘿,這完全有效,謝謝你的快速答案! – Jones123 2012-04-06 11:29:52

+0

可以使用'[bezierPath addClip]'將當前上下文剪輯到UIBezierPath,因此您不必直接操作'CGContextRef'。 – Dondragmer 2012-04-11 06:33:55

+0

完美。 ;) 謝謝 – 2013-08-09 20:06:35