I am trying to get a screen grab of a view that has a SKScene in it. The technique I am using is:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.si
A solution for Swift:
func getScreenshot(scene: SKScene, duration:TimeInterval = 0.0001, completion:((_ txt:SKTexture) -> Void)?) {
let action = SKAction.run {
let bounds = scene.view?.bounds
var image = UIImage()
UIGraphicsBeginImageContextWithOptions(bounds!.size, true, UIScreen.main.scale)
scene.view?.drawHierarchy(in: bounds!, afterScreenUpdates: true)
if let screenshot = UIGraphicsGetImageFromCurrentImageContext() {
UIGraphicsEndImageContext()
image = screenshot
} else {
assertionFailure("Unable to make a screenshot for the scene \(type(of:scene))")
}
completion!(SKTexture(image: image))
}
let wait = SKAction.wait(forDuration: duration)
let seq = SKAction.sequence([wait,action])
scene.run(seq,withKey:"getScreenshot")
}
Usage:
getScreenshot(scene: self, completion:{ (txt) -> Void in
let thumbNode = SKSpriteNode(texture: txt, size:CGSize(width:300,height:224))
// do whatever you want with your screenshot..
}