extension UIViewController {
static func takeScreenshot() -> UIImage? {
guard let topController = UIApplication.getTopController() else { return nil }
if let view = topController.view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.main.scale)
defer { UIGraphicsEndImageContext() }
if let context = UIGraphicsGetCurrentContext() {
view.layer.render(in: context)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
return screenshot
}
}
return nil
}
}
This code works perfectly, but the issue is when we have UIAlertController on top, I want to take screenshot of the entire screen (not the status bar of course) but the controller as well as any alert controller that is presented on top. Right now the code is working fine if there is no alert controller on top.
2
Answers
Here’s the function that might help
The code you are using will only take a screenshot of the top-most view controller and not the elements display on top of it.
You could instead look into taking a snapshot of the key window: