问题描述
我无法从内存中释放RealityKit ARView()
.
我知道ARKit + SceneKit也存在(曾经有过)类似的问题-解决方法如下:
I cannot manage to release my RealityKit ARView()
from memory.
I am aware that there are (were?) similar issues with ARKit + SceneKit – with workarounds like this one: https://stackoverflow.com/a/53919730/7826293which doesen´t solve my problem unfortunately.
The solutions above kind of work by removing everything "suspicious" manually. That is exactly what I did in a even wider scope:
class ViewController_ARViewMemoryTest: UIViewController {
var arView: ARView?
init() {
super.init(nibName: nil, bundle: nil)
arView = ARView(frame: .zero)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
doEverythingThatsNeeded()
}
public func doEverythingThatsNeeded() {
self.arView?.session.pause()
self.arView?.session.delegate = nil
self.arView?.removeFromSuperview()
// Quite a few more removals and resets here, ie. for Combines AnyCancellables
self.arView = nil
}
}
I am calling doEverythingThatsNeeded() from outside as well:
aRViewMemoryTest?.doEverythingThatsNeeded()
aRViewMemoryTest?.arView = nil
aRViewMemoryTest = nil
The issue seems to be independent from the fact that I have wrapped my ARView or alternatively a UIViewController in a SwiftUI UIViewRepresentable
/ UIViewControllerRepresentable
.
I believe it must be a bug and I have filed a report months ago. However I am hoping for workarounds that help until Apple fixes the potential issue.
Thanks a lot!
Try this code if you're using SwiftUI:
import SwiftUI
import RealityKit
struct ContentView : View {
var body: some View {
return ARViewContainer().edgesIgnoringSafeArea(.all)
}
}
struct ARViewContainer: UIViewRepresentable {
func updateUIView(_ uiView: ARView,
context: UIViewRepresentableContext<ARViewContainer>) {
uiView.removeFromSuperview()
}
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
let boxAnchor = try! Experience.loadBox()
arView.scene.anchors.append(boxAnchor)
return arView
}
}
...and this code if you're using UIKit:
import UIKit
import RealityKit
class ViewController: UIViewController {
var arView: ARView? = ARView(frame: .zero)
deinit {
removingView()
}
func addingView() {
self.arView?.frame = .init(x: 0, y: 0, width: 300, height: 700)
self.view.addSubview(arView!)
}
func removingView() {
self.arView?.session.pause()
self.arView?.session.delegate = nil
self.arView?.scene.anchors.removeAll()
self.arView?.removeFromSuperview()
self.arView?.window?.resignKey()
self.arView = nil
}
override func viewDidLoad() {
super.viewDidLoad()
addingView()
let boxAnchor = try! Experience.loadBox()
arView?.scene.anchors.append(boxAnchor)
removingView()
}
}
这篇关于如何取消分配RealityKit ARView()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!