簡體   English   中英

使用CALayer.mask將UIView保存為iOS 6中的UIImage

[英]Save UIView with CALayer.mask as UIImage in iOS 6

我有以下代碼:

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    UIGraphicsBeginImageContextWithOptions(mainView.bounds.size, NO, [UIScreen mainScreen].scale);
}
else {
    UIGraphicsBeginImageContext(mainView.bounds.size);
}

[mainView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

在mainView中,有一個蒙版子視圖在使用此方法時不會出現在saveImage中。 但是,據我所知,iOS 4之前的UIGetScreenImage方法確實捕獲了這樣的活動。 我的問題是,在iOS 6中捕獲CALayer活動的最佳方法是什么? UIGetScreenImage仍然是私有的嗎?

我認為一周前有一個類似的問題: 當你將uiview捕獲到uiimage時,Mask不起作用

在iOS6上,在應用蒙版的情況下捕獲UIView時出現問題(順便說一句,在iOS 7中它已被修復):您捕獲圖像但未應用蒙版。

我發布了一個冗長的解決方案,涉及手動將蒙版應用於捕獲的圖像。 這不是很困難,我也做了一個演示項目。 您可以在這里下載:

https://bitbucket.org/reydan/so_imagemask

如果我沒有正確理解您的問題,請告訴我,以便我可以刪除此答案。

嘗試獲取表示層,因為它將包含圖層的狀態。

[mainView.layer.presentationLayer renderInContext:UIGraphicsGetCurrentContext()];

https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instm/CALayer/presentationLayer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM