2014-03-01 38 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 –