编程截图黑色 - 斯威夫特

时间:2017-07-01 11:43:34

标签: swift image screenshot uiactivityviewcontroller uigraphicscontext

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

ActivityViewController似乎有效。但是,图像全黑。

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

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。

1 个答案:

答案 0 :(得分:0)

所以我将代码更改为以下内容,效果很好。

            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)