我如何初始化ARSCNView

var sceneView: ARSCNView?

override func viewDidLoad() {
    super.viewDidLoad()
    if ARConfiguration.isSupported{
        sceneView = ARSCNView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))

        view.addSubview(sceneView!)

        sceneView!.delegate = self
        // Show statistics such as fps and timing information
        sceneView!.showsStatistics = false

        // Create a new scene
        let scene = SCNScene(named: "totens.scnassets/Main.scn")!
        // Set the scene to the view
        sceneView!.scene = scene
     }
  }

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

        guard let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "TotensImages", bundle: nil) else { return }

        let configuration = ARImageTrackingConfiguration()
        configuration.trackingImages = referenceImages
        let options: ARSession.RunOptions = [.removeExistingAnchors]

        sceneView!.session.run(configuration, options: options)
 }


当关闭此视图控制器并尝试重新启动会话时,摄像机停止出现。
有人知道如何帮助我吗?谢谢

最佳答案

好的,这不是修复它的最佳方法,但可能会起作用。

在具有ARScene View的ViewController上,必须初始化一个bool标志,该标志将检查是否必须运行配置。所以..

var shouldDoThingsInViewWillAppear: Bool = true
    override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
if shouldDoThingsInViewWillAppear {
shouldDoThingsInViewWillAppear = false
        guard let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "TotensImages", bundle: nil) else { return }

        let configuration = ARImageTrackingConfiguration()
        configuration.trackingImages = referenceImages
        let options: ARSession.RunOptions = [.removeExistingAnchors]

        sceneView!.session.run(configuration, options: options)
    }

 }


如果这项工作意味着您不应运行两次会话,也不应添加两次跟踪图像。

如果不是,请尝试执行相同的viewDidAppear而不是ViewWillAppear。

如果您尝试将运行方法更改为

sceneView.session.run(configuration, options: [.resetTracking, .removeExistingAnchors])

关于ios - 回到包含ARSCNView的ViewController时,相机卡住。如何在不ARSCNView停止工作的情况下在此 View Controller 中来来去去?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54419925/

10-10 20:46