11

在我的一个 iOS 应用程序开发项目中,每当我尝试在 ARSCNView 上推送视图时。该应用程序在下面冻结是我的代码。如果您知道相同的问题,请提出适当的解决方案。

sceneView-> 在控制器上推送视图时让应用程序冻结

 let session = ARSession()
        var sessionConfig: ARSessionConfiguration = ARWorldTrackingSessionConfiguration()
        var use3DOFTracking = false {
            didSet {
                if use3DOFTracking {
                    sessionConfig = ARSessionConfiguration()
                }
                sessionConfig.isLightEstimationEnabled = true
                session.run(sessionConfig)
            }
        }    
    }

override func viewDidLoad() {
        super.viewDidLoad()
        self.setupScene()
        setupSession()
}

override func viewWillAppear(_ animated: Bool) {
        self.tabBarController?.tabBar.isHidden = false
        session.run(sessionConfig, options: [.resetTracking, .removeExistingAnchors])
    }

override func viewDidDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    session.pause()
}


 func setupScene() {
        sceneView.delegate = self
        sceneView.session = session
        sceneView.antialiasingMode = .multisampling4X
        sceneView.automaticallyUpdatesLighting = false

        sceneView.preferredFramesPerSecond = 60
        sceneView.contentScaleFactor = 1.3
        //sceneView.showsStatistics = true

        enableEnvironmentMapWithIntensity(25.0)

        if let camera = sceneView.pointOfView?.camera {
            camera.wantsHDR = true
            camera.wantsExposureAdaptation = true
            camera.exposureOffset = -1
            camera.minimumExposure = -1
        }
    }

    func setupSession() {
        if let worldSessionConfig = sessionConfig as? ARWorldTrackingSessionConfiguration {
            worldSessionConfig.planeDetection = .horizontal
            session.run(worldSessionConfig, options: [.resetTracking, .removeExistingAnchors])
        }
}
4

2 回答 2

2

不应因任何原因中断 AR 会话。尝试使用弹出屏幕重新构建您的设计。

有一个 iOS 11 Beta 错误,ARCamera当 ARSCNView 消失时,它会冻结,所以,如果你真的需要用 AR 暂时隐藏屏幕:

  1. ARSCNView从情节提要中删除并从IBOutlet视图控制器中删除;
  2. ARSCNView出现时以编程方式创建UIViewController
  3. 移除ARSCNView,当 UIViewController 消失时。

有一个简短的例子:

class ViewController: UIViewController, ARSCNViewDelegate {

    // Weak reference for scene view.
    weak var sceneView: ARSCNView!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        // Create a new AR Scene View.
        let sceneView = ARSCNView(frame: view.bounds)
        sceneView.delegate = self
        sceneView.showsStatistics = true
        view.addSubview(sceneView)
        // Create a new scene.
        sceneView.scene = SCNScene(named: "art.scnassets/ship.scn")!
        // Running session.
        sceneView.session.run(ARWorldTrackingSessionConfiguration())
        // Saving weak reference.
        self.sceneView = sceneView
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        // Removing current SceneView to avoid freezes.
        sceneView?.removeFromSuperview()
    }
}

当然,您应该检查您的框架以获取场景视图和设置ARSCNViewARSessionSCNScene根据需要进行检查。

Xcode 测试版 5

大多数冻结在 Beta 5 中已修复。但有时它仍然会冻结,例如当您使用本机共享视图控制器时。

于 2017-07-25T18:30:15.547 回答
-1

在我看来,您应该有一个 viewDidAppear 的覆盖功能并在那里启动您的 setupSession() 。

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

    // Prevent the screen from being dimmed after a while.
    UIApplication.shared.isIdleTimerDisabled = true

    // Start the ARSession.
    setupSession()
}
于 2017-07-12T00:40:22.977 回答