user8105388
user8105388

Reputation:

Take screenshot of ARKit view

How would I insert a button and use it to take a photo and place it in photo library. I have noticed when using arkit I cant drag buttons and place them over the view. I am seen some people online say you use snapshot() for taking the photo.

import UIKit
import SceneKit
import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

   @IBOutlet var sceneView: ARSCNView!

   override func viewDidLoad() {
     super.viewDidLoad()

     // Set the view's delegate
     sceneView.delegate = self

     // Show statistics such as fps and timing information
     sceneView.showsStatistics = true

     // Create a new scene
     let scene = SCNScene(named: "art.scnassets/ship.scn")!

     // Set the scene to the view
     sceneView.scene = scene
   }

   override func viewWillAppear(_ animated: Bool) {
     super.viewWillAppear(animated)

     // Create a session configuration
     let configuration = ARWorldTrackingConfiguration()

     // Run the view's session
     sceneView.session.run(configuration)
   }

   override func viewWillDisappear(_ animated: Bool) {
     super.viewWillDisappear(animated)

     // Pause the view's session
     sceneView.session.pause()
   }

   func session(_ session: ARSession, didFailWithError error: Error) {
     // Present an error message to the user
   }

   func sessionWasInterrupted(_ session: ARSession) {
     // Inform the user that the session has been interrupted, for example, by presenting an overlay 
   }

   func sessionInterruptionEnded(_ session: ARSession) {
     // Reset tracking and/or remove existing anchors if consistent tracking is required 
   }
}

Upvotes: 1

Views: 2637

Answers (1)

mugx
mugx

Reputation: 10105

I made a simple demo to show you how to combine snapshot(), ARSCNView and UIBUtton. So you may define your storyboard in this way:

enter image description here

as you can see, the button is inside the main view but outside and above the ARKit view

enter image description here

then your ViewController might be something like:

import UIKit
import ARKit

class ViewController: UIViewController {
    @IBOutlet var arkitView:ARSCNView!
    @IBOutlet var outputImageView:UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        arkitView.session.run(ARWorldTrackingConfiguration())
    }

    @IBAction func takeScreenshotAction() {
        outputImageView.image = arkitView.snapshot()
    }
}

final result is:

enter image description here

Upvotes: 1

Related Questions