How to take screen shot programmatically (Swift, SpriteKit)

后端 未结 2 1090
醉酒成梦
醉酒成梦 2021-01-02 06:52

I tried what ever was suggested but the output was a white,blank screenshot. Which leads me to assume that I haven\'t added anything to the view. Here\'s how I\'m adding gra

2条回答
  •  再見小時候
    2021-01-02 07:15

    Here is how I solved it in swift 3: This captures all the nodes in your scene: Note: in below code represents the view it is capturing from. You may want to update it with your view in project.

    func captureScreen() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(yourview.bounds.size, true, 0)
    
        yourview.drawHierarchy(in: yourview.bounds, afterScreenUpdates: true)
    
    
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
    

提交回复
热议问题