UIScreen上的snapshotViewAfterScreenUpdates在设备上显示空白快照

时间:2015-01-06 11:07:43

标签: ios ios8 uiscreen

我正在尝试在用户看到整个屏幕时获取图像。以下代码应该可以工作,但它只能在模拟器中工作。我该怎么做才能让它在运行iOS 8.1的设备上运行?

UIView *snapshot = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];
CGSize outputSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
UIGraphicsBeginImageContextWithOptions(outputSize, NO, 0);
[snapshot drawViewHierarchyInRect:CGRectMake(0.0, 0.0, outputSize.width, outputSize.height) afterScreenUpdates:YES];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

很高兴尝试其他策略,但关键是我希望用户看到它 - 键盘和所有。

干杯

1 个答案:

答案 0 :(得分:1)

UIGraphicsBeginImageContextWithOptions(CGSizeMake(CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)), NO, 1.0f);
        [self.view drawViewHierarchyInRect:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) afterScreenUpdates:NO];
        UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();