2014-03-01 30 views
0

我需要渲染与透明矩形uiview。结果,该uiview被设置为掩码并呈现它。以下代码适用于ios 7.但是,它不适用于ios 6.我无法使用透明/空白框获取图像。我该怎么办?uiview设置掩码不好在ios 6.1

CAShapeLayer *mask = [[CAShapeLayer alloc] init]; 
mask.frame = self.view1.layer.bounds; 
CGRect biggerRect = CGRectMake(mask.frame.origin.x, mask.frame.origin.y, mask.frame.size.width, mask.frame.size.height); 


CGRect smallerRect = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f); 

UIBezierPath *maskPath = [UIBezierPath bezierPath]; 
[maskPath moveToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMinY(biggerRect))]; 
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMaxY(biggerRect))]; 
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(biggerRect), CGRectGetMaxY(biggerRect))]; 
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(biggerRect), CGRectGetMinY(biggerRect))]; 
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(biggerRect), CGRectGetMinY(biggerRect))]; 

[maskPath moveToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMinY(smallerRect))]; 
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMaxY(smallerRect))]; 
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(smallerRect), CGRectGetMaxY(smallerRect))]; 
[maskPath addLineToPoint:CGPointMake(CGRectGetMaxX(smallerRect), CGRectGetMinY(smallerRect))]; 
[maskPath addLineToPoint:CGPointMake(CGRectGetMinX(smallerRect), CGRectGetMinY(smallerRect))]; 

mask.path = maskPath.CGPath; 
[mask setFillRule:kCAFillRuleEvenOdd]; 
mask.fillColor = [[UIColor blackColor] CGColor]; 

// Set the mask of the view. 
_view1.layer.mask = mask; 





UIGraphicsBeginImageContextWithOptions(_view1.bounds.size, NO, 0.0); 
[_view1.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); 

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 

NSString *documentsDirectory = [path objectAtIndex:0]; 

NSString *myDBnew = [documentsDirectory stringByAppendingPathComponent:@"renderImg.png" ]; 

if ([[NSFileManager defaultManager] fileExistsAtPath:myDBnew]) { 
    [[NSFileManager defaultManager] removeItemAtPath:myDBnew error:nil]; 
} 


NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory1 = [paths1 objectAtIndex:0]; 
NSString *savedImagePath1 = [documentsDirectory1 stringByAppendingPathComponent:[NSString stringWithFormat:@"renderImg.png"]]; 
NSData *imageDatatest = UIImagePNGRepresentation(UIGraphicsGetImageFromCurrentImageContext()); 
[imageDatatest writeToFile:savedImagePath1 atomically:NO]; 




UIGraphicsEndImageContext(); 

_imgView.image = img; 

http://i58.tinypic.com/33ti8v5.png

变形
掩蔽视图或消灭与透明矩形区域下面描述的原因。 我需要将这两个uiview渲染到一起,如第二个图像所示,但我无法接收到所需的图像。它看起来像透明视图无法在具有颜色的另一个视图上呈现。因此,我想先消灭那些区域并用第二个视图渲染它。我该怎么办? enter image description here

+0

图像都显示ios7? –

+0

是的。都显示ios7。它不是在iOS 6中可以。 –

+0

你能提供一个最小样本,所以我可以尝试运行它吗? –

回答

1

我试了一下代码,可以确认它是正确的,但是在离子和下面不起作用。

我读renderInContext:的文档,发现它甚至会告诉你:
“:目前这个方法没有实现充分CoreAnimation组合模型,谨慎使用警告。”

我交叉引用的OSX文档和更为明确:
“重要 的OS X v10.5上实现此方法不支持整个Core Animation的组合模型的QCCompositionLayer,CAOpenGLLayer和QTMovieLayer层都没有。此外,使用3D变换的图层不会被渲染,也不会渲染指定backgroundFilters,filters,compositingFilter或mask值的图层。未来版本的OS X可能会添加对渲染这些图层和属性的支持。

=>这就是为什么它适用于ios7而不是ios6。


一个解决方法是将面膜不仅适用于层内,而是再次向ImageContext

//mask layer to image 
UIGraphicsBeginImageContextWithOptions(_view1.bounds.size, NO, 0.0); 
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, _view1.bounds.size.height); 
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1, -1); 
[_view1.layer.mask renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage * maskImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

//draw img 
UIGraphicsBeginImageContextWithOptions(_view1.bounds.size, NO, 0.0); 
CGContextClipToMask(UIGraphicsGetCurrentContext(), _view1.bounds, maskImage .CGImage); 
[_view1.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+0

哦..感谢您的帮助。我希望苹果能够修复。我想我需要渲染uview。我增加了以透明的视角清除这些区域的目的。我该怎么办? –

+0

?对不起,我不遵循...只是添加代码,而不是你的,它呈现良好的形象... –

+0

的事情是,我不仅适用于该图层的面膜,但UIImage –