如何取消分配RealityKit ARView()? [英] How to deallocate RealityKit ARView()?

查看:100
本文介绍了如何取消分配RealityKit ARView()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从内存中释放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/7826293 which 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()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆