截图在模拟器中工作但在真实设备上失败

时间:2017-05-12 19:29:17

标签: ios swift xcode sprite-kit

我有以下代码,它会截取游戏画面的截图,然后在点击“分享”按钮时分享它。

它在模拟器上运行良好并返回正确的图像。但是,在我的真实设备上,它会返回一个白色图像。每一次。

以下是代码:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    let Touch : UITouch! = touches.first
    let TouchLocation = Touch.location(in: self)

    if let body = self.physicsWorld.body(at: TouchLocation){

        if body.node!.name == "shareButton" {
                let postText: String = "Check out my score! Can you beat it?"
                let postImage: UIImage = getScreenshot()
                let activityItems = [postText as AnyObject, postImage] //as [Any]
                let activityController = UIActivityViewController(
                    activityItems: activityItems,
                    applicationActivities: nil
                )

                let controller: UIViewController = scene!.view!.window!.rootViewController!

                controller.present(
                    activityController,
                    animated: true,
                    completion: nil
                )
        }
    }
}

这是功能:

func getScreenshot() -> UIImage {
    let snapshotView = self.view!.snapshotView(afterScreenUpdates: true)
    let bounds = UIScreen.main.bounds

    UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)

    snapshotView?.drawHierarchy(in: bounds, afterScreenUpdates: true)

    let screenshotImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()!

    UIGraphicsEndImageContext()

    return screenshotImage;
}

我将问题缩小到这些代码行:

let postText: String = "Check out my score! Can you beat it?"
let postImage: UIImage = getScreenshot()
let activityItems = [postText as AnyObject, postImage] //as [Any]
let activityController = UIActivityViewController(
    activityItems: activityItems,
    applicationActivities: nil
)

let controller: UIViewController = scene!.view!.window!.rootViewController!

controller.present(
    activityController,
    animated: true,
    completion: nil
)

func getScreenshot() -> UIImage {
    let snapshotView = self.view!.snapshotView(afterScreenUpdates: true)
    let bounds = UIScreen.main.bounds

    UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)

    snapshotView?.drawHierarchy(in: bounds, afterScreenUpdates: true)

    let screenshotImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()!

    UIGraphicsEndImageContext()

    return screenshotImage;
}

请注意我只使用一节课。这一个:

class GameScene: SKScene {
}

我通过创建一个新的应用程序缩小了它。除了添加这些代码行之外,我没有更改任何默认的Hello World应用程序。它们返回相同的结果:空白图像。

感谢所有帮助!

2 个答案:

答案 0 :(得分:0)

以下是我用于截屏的代码。最大的区别是我对于afterScreenUpdates你的情况是假的:

public extension UIView {
    public func snapshotImage() -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0)
        drawHierarchy(in: bounds, afterScreenUpdates: false)
        let snapshotImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return snapshotImage
    }

    public func snapshotView() -> UIView? {
        if let snapshotImage = snapshotImage() {
            return UIImageView(image: snapshotImage)
        } else {
            return nil
        }
    }
}

答案 1 :(得分:0)

根据Apple的回复,出于安全原因,我们无法这样做。 https://developer.apple.com/forums/thread/108278?answerId=613818022#613818022