6

我正在寻找一种在包含顶部状态栏的 iPhone 上捕获屏幕截图的方法,我目前正在使用以下代码:

    UIGraphicsBeginImageContext(self.view.bounds.size); //self.view.window.frame.size
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

上面的代码成功截取了 iPhone UIView 的屏幕截图,但不包括顶部状态栏(取而代之的是一个空白的 20px 空间)。

4

4 回答 4

11

从 iOS 7 开始,您可以在不使用私有 API 的情况下使用

UIView *screenshotView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO];

看我对这个问题的回答:

在 iOS 7 中移动状态栏

于 2013-09-25T17:45:21.693 回答
3

您可以通过调用私有 API 来获取屏幕的全部内容UIGetScreenImage。有关详细信息,请参阅我之前类似问题的回答。

于 2009-08-18T00:02:30.487 回答
1

截至 2009 年 10 月 8 日,使用 UIGetScreenImage 让我的应用程序被拒绝!:( 我不建议使用它。我相信 Apple 正在尝试清理所有应用程序并使其符合新的 3.x OS/API。我正在寻找替代方案。如果有人有任何建议。视频应用程序接口?

于 2009-10-09T16:19:35.833 回答
1

为什么不将整个 UIWindow 渲染到图像上下文中,而不是使用私有 API?在您的代码中self.view替换为可能就足够了。self.view.window

您还可以获取当前窗口作为[UIApplication sharedApplication]实例的属性。状态栏可能位于单独的窗口层上,也许您必须按顺序渲染窗口。

像这样的东西:

UIGraphicsBeginImageContext(self.view.window.frame.size);
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
   [window.layer renderInContext:UIGraphicsGetCurrentContext()];
}
UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

无论如何,您可能不需要求助于私有 API。

于 2009-11-15T17:23:58.660 回答