回到包含ARSCNView的ViewController时,相机冻结。如何在没有ARSCNView停止工作的情况下在此视图控制器中进出?

时间:2019-01-29 11:21:04

标签: ios swift arkit arscnview

我如何初始化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)
 }

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

1 个答案:

答案 0 :(得分:0)

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

在具有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。

如果想尝试将run方法更改为

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