简体   繁体   English

使用view.layer.cornerRadius拍摄视图的屏幕截图

[英]Take screenshot of views with view.layer.cornerRadius

I am using the following code to take a screenshot: 我正在使用以下代码进行屏幕截图:

UIGraphicsBeginImageContext(contentView.frame.size);
[contentView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, self,@selector(savedImage:didFinishSavingWithError:contextInfo:),nil);

The problem with this is that it seems not to capture my UILabel's correctly that I have with rounded corners. 问题是似乎无法正确捕获带有圆角的UILabel's

lbl.layer.cornerRadius=radius;

This is how it should be: 这应该是这样的:

在此处输入图片说明

and this is how it appears when the view is captured: 这是捕获视图时的样子:

在此处输入图片说明

Whats goin on?? 发生了什么事?? I've tried a combination of maskToBounds and clipsToBounds but nothing is working, maybe there is an alternate way that can capture the view with rounded corners. 我尝试了maskToBoundsclipsToBounds的组合,但是没有任何效果,也许还有另一种方法可以捕获带有圆角的视图。

Thanks for any advice! 感谢您的任何建议!

Try with the bounds of your view while initialising your UIGraphicsBeginImageContext . 初始化UIGraphicsBeginImageContext时,尝试使用视图的边界

For Ex: 例如:

 UIGraphicsBeginImageContextWithOptions(contentView.bounds.size, NO, 1.0); //retina res

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

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