编程截图变黑 - Swift

Screenshot by programming turns black - Swift

我正在尝试通过 UIUIActivityViewController 分享我的游戏截图。为此,我以编程方式创建屏幕截图并将其传递给 UIActivityViewController。

A​​ctivityViewController 似乎可以工作。但是,图像全黑。

这是我的代码,我错过了吗?

UIGraphicsBeginImageContextWithOptions(view!.frame.size, false, 0.0)
view!.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

let shareViewController = UIActivityViewController(activityItems: [image], applicationActivities: [])
MenuScene.parentView.present(shareViewController, animated: true, completion: nil)

MenuScene.parentView 是 GameViewController。

所以我把代码改成下面这样,效果很好。

            let bounds = UIScreen.main.bounds
            UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0)
            yourUIViewController.view!.drawHierarchy(in: bounds, afterScreenUpdates: false)

            let img = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()

            let activityViewController = UIActivityViewController(activityItems: [img!], applicationActivities: nil)

当然还有展示观点。

yourUIViewController.present(activityViewController, animated: true, completion: nil)