我如何初始化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视图的ViewController上,您必须初始化一个布尔标志,该标志将检查您是否必须运行配置。
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 而不是 ViewWillShow 执行相同的操作。
如果下界尝试将运行方法更改为
sceneView.session.run(configuration, options: [.resetTracking, .removeExistingAnchors])