Can't capture masks within view layer

2019-03-01 09:58发布

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

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.

Any ideas? Thank you.

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-01 10:28

It's unfortunate, but that's just how it works. The documentation for -[CALayer renderInContext:] says:

... layers that use 3D transforms are not rendered, nor are layers that specify backgroundFilters, filters, compositingFilter, or a mask values.

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.

查看更多
登录 后发表回答