Search code examples
swiftarscnview

ScreenShot issue in ARKit in swift


i have application that uses ARSCNView. i'm trying to take a screenshot on click of a button and saved that image in the gallery. But when i take a screenshot it does not show the content on that screen. Just show that image, i have some labels on it but it does not show that in an image. This is my code,

  @IBAction func captureImage(_ sender: Any) {


 image = sceneView.snapshot()
     UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
}

How can i show that labels and buttons on ARSCView in a screenshot?


Solution

  • snapshot() will only take screenshot of Scene.

    To take screenshot of Scene with Labels and Buttons use below method:

     func snapshot(of rect: CGRect? = nil) -> UIImage? {
            UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.isOpaque, 0)
            self.view.drawHierarchy(in: self.view.bounds, afterScreenUpdates: true)
            let fullImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            guard let image = fullImage, let rect = rect else { return fullImage }
            let scale = image.scale
            let scaledRect = CGRect(x: rect.origin.x * scale, y: rect.origin.y * scale, width: rect.size.width * scale, height: rect.size.height * scale)
            guard let cgImage = image.cgImage?.cropping(to: scaledRect) else { return nil }
            return UIImage(cgImage: cgImage, scale: scale, orientation: .up)
        } 
    

    To Take Screenshot:

     @IBAction func takeScreenShotTapped(_ sender: Any) {
        let screenShot = snapshot(of: CGRect(x: 80, y: 80, width: 100, height: 100))
    }
    

    If you want to take screenshot of fullscreen then just call snapshot().

    Hope this will help you :)