Swift iOS整个屏幕的屏幕截图,包括拆分视图

时间:2015-11-23 00:32:31

标签: ios image swift screenshot

我一直在试图弄清楚如何截取应用程序视图的屏幕截图/屏幕截图,而不是设备的完整屏幕。

主要用例是在iOS 9中使用新的多任务功能,并拍摄另一个正在运行的应用程序的屏幕截图。

非常感谢任何帮助或指导。

let theView = view
var screenSize = UIScreen.mainScreen().bounds.size

if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation) {
   screenSize = CGSizeMake(screenSize.height, screenSize.width)
}

UIGraphicsBeginImageContextWithOptions(screenSize, true, 0.0)

theView.drawViewHierarchyInRect(theView.bounds, afterScreenUpdates: true)

let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return image

1 个答案:

答案 0 :(得分:3)

Press the Home and Sleep/wake buttons at the same time.

至于以编程方式进行,我认为你运气不好。应用无法访问其他进程绘制的屏幕内容。这大概是通过设计,作为保护应用程序远离彼此沙箱的安全措施的一部分。