How to take a layer's snapshot with mask layer?

巧了我就是萌 提交于 2020-01-04 04:54:23

问题


I added a mask to UIView's layer:

CGImageRef maskImageRef = [UIImage imageNamed:"Icon.png"].CGImage; 
CALayer maskLayer = [CALayer layer];
maskLayer.contents = (__bridge id)maskImageRef;
self.layer.mask = maskLayer;

Then I use this code to get snapshot from a UIView:

[self.layer renderInContext:mainViewContentContext];

But the mask wasn't drawn.

How to draw self.layer with mask?

Special thx!


回答1:


One way to fix this can be to use Quartz functions

Use CGContextClipToMask(context, rect, maskImage) inside drawRect method

Refer CGContextClipToMask example link for help




回答2:


You have to screenshot of the screen try this code:

CGRect screenRect =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-44); 
UIGraphicsBeginImageContext(screenRect.size);

CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:ctx];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil);

i hope help you.



来源:https://stackoverflow.com/questions/11076534/how-to-take-a-layers-snapshot-with-mask-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!