我正在尝试获取另一个控制器视图的快照以在动画中使用。我正在使用以下代码,但我只看到该视图的背景颜色,以及它的任何子视图(两个按钮、一个文本视图和一个标签):
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
FirstPageController *fpController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstPage"];
UIGraphicsBeginImageContext(fpController.view.bounds.size);
NSLog(@"%@",fpController.view.subviews);
[fpController.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGSize destinationSize = CGSizeMake(90, 122);
UIGraphicsBeginImageContext(destinationSize);
[viewImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.imageView2 = [[UIImageView alloc] initWithImage:newImage];
self.imageView2.center = self.view.center;
[self.view addSubview:self.imageView2];
NSLog(@"%@",NSStringFromCGSize(self.imageView2.image.size));
}
我已经记录了所有相关的东西,fpController、imageView、图像,所有的记录都正确。
编辑后:如果我切换到当前控制器的视图,它工作正常,所以我认为这与获取不在屏幕上的视图的快照有关。可以这样做吗?