使用 Swift 5 returns 黑色图像而非应用程序屏幕截图

Taking screenshot using Swift 5 returns a black image and not the app's screen

我正在尝试截取我的应用程序的屏幕截图然后分享。

我遇到的问题是,当我截取屏幕截图并将其保存到照片应用程序时,保存的图像是全黑的(而不是我的应用程序的屏幕截图)。

如何获取应用程序的屏幕截图?我错过了什么?

func takeScreenshot() {
        let bounds = UIScreen.main.bounds
        UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0)
        self.view.drawHierarchy(in: bounds, afterScreenUpdates: false)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        let activityViewController = UIActivityViewController(activityItems: [img!], applicationActivities: nil)
        self.present(activityViewController, animated: true, completion: nil)
    }

应用是单屏。
Swift 5
xCode10

我试过浏览其他类似的 Whosebug 问题并将我的代码调整为他们的代码,但得到了相同的结果。撞墙试试这里

感谢您的帮助!

尝试改变

 afterScreenUpdates: false

 afterScreenUpdates: true

这样就会有一些图纸快照。