本文介绍了将非活动UIView捕获为UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码运行良好,但仅捕获屏幕上可见的UIView.我该如何获取当前未显示的UIView?

The code below works well but grabs only the UIView which is VISIBLE on your screen.How can I grab instead a UIView which is not currently displaying?

谢谢!

    //Take a screenshot of the view...
UIGraphicsBeginImageContext(View_1.frame.size);
    [View_1.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *View_1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(View_1, nil, nil, nil);

//...and attach it as an image to the email
NSData *myData3 = UIImagePNGRepresentation(View_1);
[picker addAttachmentData:myData3 mimeType:@"image/png" fileName:@"screenshot2"];   

推荐答案

如果您的意思是像view.hidden = YES;中那样隐藏,则将无法绘制它,或者将其隐藏,也可以将其从超级视图中删除,甚至在绘制前调用view.hidden = NO;,然后在绘制后调用view.hidden = YES;

If you mean hidden like in view.hidden = YES; then you will not be able to draw it, instead of hiding it you can either remove it from the super view, or even call view.hidden = NO; before drawing it and then view.hidden = YES; after it has been drawn

示例

//Take a screenshot of the view...
UIGraphicsBeginImageContext(View_1.frame.size);
//Set it to visible
View_1.hidden = NO;
[View_1.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *View_1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(View_1, nil, nil, nil);
//Hide it again
View_1.hidden = YES;
//...and attach it as an image to the email
NSData *myData3 = UIImagePNGRepresentation(View_1);
[picker addAttachmentData:myData3 mimeType:@"image/png" fileName:@"screenshot2"];

这篇关于将非活动UIView捕获为UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 21:30