简体   繁体   English

使用CALayer.mask将UIView保存为iOS 6中的UIImage

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

I have the following code: 我有以下代码:

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();

In mainView, there is a masked subview that does not appear in saveImage when using this method. 在mainView中,有一个蒙版子视图在使用此方法时不会出现在saveImage中。 However, I understand there used to be a UIGetScreenImage method pre iOS 4 that did capture such activity. 但是,据我所知,iOS 4之前的UIGetScreenImage方法确实捕获了这样的活动。 My question is, what is the best way to capture CALayer activities in iOS 6? 我的问题是,在iOS 6中捕获CALayer活动的最佳方法是什么? Is UIGetScreenImage still private? UIGetScreenImage仍然是私有的吗?

I think there was a similar question about a week ago: Mask does not work when capturing a uiview to a uiimage 我认为一周前有一个类似的问题: 当你将uiview捕获到uiimage时,Mask不起作用

On iOS6 there is a problem capturing a UIView with the mask applied (btw, in iOS 7 it has been fixed): you capture the image but the mask is not applied. 在iOS6上,在应用蒙版的情况下捕获UIView时出现问题(顺便说一句,在iOS 7中它已被修复):您捕获图像但未应用蒙版。

I posted a lengthy solution which involved applying the mask manually to the captured image. 我发布了一个冗长的解决方案,涉及手动将蒙版应用于捕获的图像。 It's not very difficult and I also made a demo project of this. 这不是很困难,我也做了一个演示项目。 You can download it here: 您可以在这里下载:

https://bitbucket.org/reydan/so_imagemask https://bitbucket.org/reydan/so_imagemask

If I did not understand your problem correctly, please tell me so I can remove this answer. 如果我没有正确理解您的问题,请告诉我,以便我可以删除此答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM