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

7

我想要获取整个屏幕以用户所见的方式呈现的图像。以下代码应该可以工作,但只能在模拟器中工作。我应该怎么做才能让它在运行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个回答

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

不幸的是,这并不是那么简单。我想要整个屏幕就像用户看到的那样 - 这会渲染一个视图,这意味着即使您使用根视图,您仍将缺少状态栏和键盘(因此我正在尝试使用UIScreen的方法)。 - Kieran Harper
对于那些使用谷歌搜索的人,这个方法解决了我在WKWebView上遇到的黑屏截图问题。 - ninjaneer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接