无法在视图层中捕获蒙版 [英] Can't capture masks within view layer
问题描述
因此,我使用以下代码将图像蒙版应用于UIView层:
So I'm applying an image mask to a UIView layer with the following code:
CALayer *maskLayer = [CALayer layer];
UIImage *maskImage = self.image.image;
maskLayer.contents = (id)maskImage.CGImage;
maskLayer.frame = CGRectMake(0.0, 0.0,1235, 935);
self.bgView.layer.mask = maskLayer;
一切都很好,花哨的东西,遮罩覆盖了视图的内容并起作用。但是,我正在尝试截屏以使用户保存图像。我正在使用以下代码:
Everything is fine and dandy, the mask covers the view content and works. However, I am trying to take a screenshot to let the user save the image. I'm using the following code:
UIGraphicsBeginImageContext(captureFrame.size);
[self.bgView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
相框很好,可以保存到照片库中。唯一的问题是应用于该图层的蒙版没有显示在JPG中。我还尝试将bgView嵌套到另一个UIView中,然后尝试 renderInContext:
,但这是相同的。看起来好像没有在最终的JPG中应用蒙版。
The frame is fine, and its saving to the photo library. The only problem is that the mask applied to the layer does not show up in the JPG. I have also tried nesting my bgView into another UIView, and then trying to renderInContext:
that, but its the same thing. It simply appears as if the mask is not being applied in the final JPG.
有什么想法吗?谢谢。
推荐答案
很不幸,但这就是它的工作原理。 -[CALayer renderInContext:]
的文档说:
It's unfortunate, but that's just how it works. The documentation for -[CALayer renderInContext:]
says:
...不会渲染使用3D变换的图层,也不会渲染指定backgroundFilters,filters,compositingFilter或mask值的图层。
... layers that use 3D transforms are not rendered, nor are layers that specify backgroundFilters, filters, compositingFilter, or a mask values.
CoreAnimation用于屏幕上的动画绘图;它不是通用图形框架。改为尝试使用CoreGraphics-没那么困难,您将拥有更多控制权,并且您可以获得更高质量的输出。
CoreAnimation is intended for on-screen, animated drawing; it isn't a general-purpose graphics framework. Try using CoreGraphics instead -- it isn't that difficult, you'll have more control, and you'll get higher-quality output.
这篇关于无法在视图层中捕获蒙版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!