我需要渲染与透明矩形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渲染到一起,如第二个图像所示,但我无法接收到所需的图像。它看起来像透明视图无法在具有颜色的另一个视图上呈现。因此,我想先消灭那些区域并用第二个视图渲染它。我该怎么办?
图像都显示ios7? –
是的。都显示ios7。它不是在iOS 6中可以。 –
你能提供一个最小样本,所以我可以尝试运行它吗? –