2014-03-25 78 views
1

我想用iOS中的另一個圖像填充圖像的透明部分。UIImage用另一個圖像填充透明部分

我嘗試了一些東西UIGraphicsContext,但我無法得到它的工作,因爲我從來沒有使用過。

以下是圖像:

enter image description here

一些代碼,我嘗試:

UIImageView *v = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 70, 70)]; 

UIGraphicsBeginImageContextWithOptions(v.frame.size, YES, 0); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
// beach image 
UIImage *img = [UIImage imageNamed:@"zivogosce_camping_05.jpg"]; 
//[img drawInRect:CGRectMake(2, 0, 12, 12)]; 

UIImage *annotationImg = [UIImage imageNamed:@"custom_annotation"]; 
[annotationImg drawAtPoint:CGPointMake(0, 0)]; 

CGContextSetFillColorWithColor(context, [[UIColor colorWithPatternImage:img] CGColor]); 
CGContextFillRect(context, CGRectMake(0, 0, 50, 50)); 


CGContextDrawImage(context, CGRectMake(0, 0, v.frame.size.width, v.frame.size.height), [annotationImg CGImage]); 



UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 

[v setImage:newImage]; 

UIGraphicsEndImageContext(); 

但圖像不合身......

+2

你爲什麼不在其他圖像上繪製它? –

+0

請張貼一些代碼,以便我們可以幫助您確定問題所在。你現在怎麼畫? – user1118321

回答

1

這是我會怎麼做:

您可以通過做圓形的剪貼路徑如下所示:

CGContextSaveGState(context); 
CGRect circleRect = CGRectMake(circleCenter.x - radius, circleCenter.y - radius, radius * 2.0, radius * 2.0); 
CGContextAddEllipseInRect (context, circleRect); 
CGContextClip(context); 

// ... do whatever you need to in order to draw the inner image ... 

CGContextRestoreGState(context); 

// ... draw the transparent png ... 

設置剪切路徑將導致繪圖只發生在路徑中。當你恢復狀態時,它將刪除剪切路徑(或者將其設置回其先前的值,通常允許它繪製到整個畫布)。

+0

請舉例 – John