SpriteKit screenshot of view off screen

2019-07-09 05:52发布

问题:

I am creating a vertical scrolling game in SpriteKit and I would like to take a screenshot of the entire game scene once the game ends including the parts that have scrolled off screen. So far I am only able to capture the part of the screen that is visible using the following:

    let bounds = self.scene!.view?.bounds
    UIGraphicsBeginImageContextWithOptions(bounds!.size, true, UIScreen.mainScreen().scale)
    self.scene?.view!.drawViewHierarchyInRect(bounds!, afterScreenUpdates: true)
    screenshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

Is it possible to get the entire scene?

Thanks.

回答1:

I was seeing your code and I don't see any problem at all; in fact I tested it (made it a function) and it works perfectly .

func getScreenshot(scene: SKScene) -> UIImage {
    let bounds = self.scene!.view?.bounds
    UIGraphicsBeginImageContextWithOptions(bounds!.size, true, UIScreen.mainScreen().scale)
    self.scene?.view!.drawViewHierarchyInRect(bounds!, afterScreenUpdates: true)
    let screenshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return screenshot;
}

I've tested it myself as it is and works; hope it helps :)