0

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

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

4

1 回答 1

1

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

在您拥有 ARScene 视图的 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])
于 2019-01-29T13:02:05.967 回答