ios - UIScreen 上的 snapshotViewAfterScreenUpdates 在设备上提供空白快照

标签 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();

很高兴尝试替代策略,但关键是我想要用户看到它的样子 - 键盘和所有。

干杯

最佳答案

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();

关于ios - UIScreen 上的 snapshotViewAfterScreenUpdates 在设备上提供空白快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27797360/

相关文章:

ios - UINavigationController 导航栏上方的空间

android - 为每个用户生成唯一 ID

ios - 如何使用 WKWebView 检索文件?

ios - 如何在导航 Controller 中将数据从一个 View 传递到另一个 View

ios - 圆形 View 上的手势识别器

swift - 在 SWIFT 中使用 UIDatePicker(在 UIActionSheet 中)输入日期时 UITextField 中的日期格式

ios - 将数据从 TableView 传递到 View Controller

ios5 - 在 iOS 5 中获取屏幕亮度值

ios - 使用 Swift 调整主屏幕亮度

iphone - 在 iOS 上,我们可以从 UIScreen 对象到达主 UIWindow 对象吗?