获取 ARKit 视图的屏幕截图

Take screenshot of ARKit view

我如何插入一个按钮并使用它来拍摄照片并将其放入照片库。我注意到在使用 arkit 时我无法拖动按钮并将它们放在视图上。我在网上看到有人说你用snapshot()来拍照。

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 
   }
}

我做了一个简单的演示,向您展示如何组合 snapshot()ARSCNViewUIBUtton。所以你可以这样定义你的故事板:

如您所见,按钮在主视图内,但在 ARKit 视图之外和上方

那么你的 ViewController 可能是这样的:

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()
    }
}

最终结果是: