0

I'm trying to take a screenshot of a SCNView to display elsewhere. Since SCNView inherits from UIView, I thought I could use a UIGraphicsImageRenderer:

extension UIView {

    // Using a function since `var image` might conflict with an existing variable
    // (like on `UIImageView`)
    func asImage() -> UIImage {
        let renderer = UIGraphicsImageRenderer(bounds: bounds)
        return renderer.image { rendererContext in
            layer.render(in: rendererContext.cgContext)
        }
    }
}

When I attempt to use this code on an SCNView, it causes a blank (white) UIImage (of the correct bounds).

How do I take a screenshot of a SCNView correctly?

Senseful
  • 86,719
  • 67
  • 308
  • 465

1 Answers1

0

Instead of using a UIGraphicsImageRenderer to get a screenshot, just call the snapshot method on SCNView:

let image = sceneView.snapshot()
Senseful
  • 86,719
  • 67
  • 308
  • 465