我需要渲染與透明矩形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中可以。 –
你能提供一個最小樣本,所以我可以嘗試運行它嗎? –