2012-01-24 132 views
1

我目前呈现视图的可见部分的PNG,像这样:如何呈现视图及其子视图的交集?

UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale); 
[self.layer renderInContext:UIGraphicsGetCurrentContext()]; 
image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

视图有一个子视图名为ImageView的(和其他一些);我想呈现与imageView相交的视图的可见部分(包括其他子视图)。我正在寻找使用UIRectClip,但我得到非常奇怪的结果。如何渲染视图和其子视图的交集?

回答

0

原来,我需要使用CGContextTranslateCTM,像这样:

CGRect combinedFrame = CGRectIntersection(imageView.frame, self.bounds); 
UIGraphicsBeginImageContextWithOptions(combinedFrame.size, NO, [UIScreen mainScreen].scale); 
CGContextRef ctx = UIGraphicsGetCurrentContext();  
CGContextTranslateCTM(ctx, -combinedFrame.origin.x, -combinedFrame.origin.y); 
[self.layer renderInContext:UIGraphicsGetCurrentContext()]; 
image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
0

您是否尝试过使用layer.mask属性?你可以说掩盖使用另一种观点认为一个观点:

viewA.layer.mask = viewB.layer. 

(你需要#IMPORT并添加QuartzCore框架来使用这些属性)。

相关问题