[英]Screenshot of an alert, iOS, Swift
I am taking a screenshot when I show an alert, this works in taking a screenshot but when it is saved to the camera roll it has only captured the screen behind the alert and hasn't included the alert in the screenshot. 我在显示警报时正在截取屏幕快照,这可以截取屏幕快照,但是当将其保存到相机胶卷时,它仅捕获了警报背后的屏幕,并且没有将警报包括在屏幕快照中。
Is it possible to do this? 是否有可能做到这一点?
Below is my code. 下面是我的代码。
func ScreenShot() {
//Create the UIImage
UIGraphicsBeginImageContextWithOptions(view.frame.size, true, 0)
guard let context = UIGraphicsGetCurrentContext() else { return }
view.layer.render(in: context)
guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return }
UIGraphicsEndImageContext()
//Save it to the camera roll
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
caling it here. 在这里校准。
func alertShow() {
let alert = UIAlertController(title: "ALERT!!!", message: "Message stuff", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { action in
self.performSegue(withIdentifier: "toMainInfoVC", sender: nil)
}))
self.present(alert, animated: true, completion: nil)
ScreenShot()
}
I think you need the complete layer of the key window 我认为您需要密钥窗口的完整层
let layer = UIApplication.shared.keyWindow!.layer
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
layer.render(in: UIGraphicsGetCurrentContext()!)
There are two problems with your code. 您的代码有两个问题。 First you are immediately trying to take a snapshot before the alert has finished being presented. 首先,在警报显示完毕之前,您立即尝试拍摄快照。 You shoudl call your snapshot function in the compeletion handler of present: 您应该在present的compeletion处理程序中调用快照函数:
self.present(alert, animated: true) { self.ScreenShot() }
Second you are only rendering the current view, and the alert is not a subview of your viewcontroller's view. 其次,您仅呈现当前视图,并且警报不是您的viewcontroller视图的子视图。 You want to render the entire window hierarchy instead: 您想改为呈现整个窗口层次结构:
view.window?.layer.render(in: context)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.