截取当前不在iOS屏幕上的视图

我试图在两个ViewController之间进行转换。 我的Transition类有一个目标视图控制器的属性。 当我尝试获取目标视图的截图时,我使用这种方法:

+ (UIImage *)renderImageFromView:(UIView *)view withRect:(CGRect)frame { // Create a new context the size of the frame UIGraphicsBeginImageContextWithOptions(frame.size, YES, 0); CGContextRef context = UIGraphicsGetCurrentContext(); // Render the view [view.layer renderInContext:context]; // Get the image from the context UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext(); // Cleanup the context you created UIGraphicsEndImageContext(); return renderedImage; } 

所以当我想要图像时,我会这样做:

 UIImage *image = [UIImage renderImageFromView:destinationController.view withRect:destinationController.view.bounds]; 

我尝试了一个空白的UIViewController与橙色背景颜色和标签。 我得到橙色的背景颜色,但我没有得到在IB创build的标签。 有没有办法获得我计划展示的新视图的正确截图? 谢谢!

您需要确保首先绘制视图的图层。 在CALayer上调用renderInContext只会recursion调用其他子CALayer 。 你需要你的孩子UIView来绘制自己,而不仅仅是使用CALayer

尝试打电话

 [view drawrect:frame]; 

代替

 [view.layer renderInContext:context]; 

只要你有一个开放的graphics上下文(你在这一点上), drawrect应该直接绘制到。