当我返回到包含 ARSCNView 的 viewcontroller 时,相机冻结。如何在没有 ARSCNView 停止工作的情况下在这个视图控制器中来来去去?
Camera freezes when I come back to viewcontroller containing ARSCNView. How to come and go in this view controller without ARSCNView stop working?
我如何初始化 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 上,您必须初始化一个 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])
我如何初始化 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 上,您必须初始化一个 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])