removing SCNNode does not free memory before creating new SCNNode

后端 未结 1 1411
难免孤独
难免孤独 2021-01-13 02:18

I am building an app that uses Scenekit to display a scene based on information returned from a data base. I have created a custom class with a class func that creates a SCN

相关标签:
1条回答
  • 2021-01-13 02:48

    I had the same problem, with my SceneKit app leaking a lot of memory. The memory of the SCNNode is freed if you set its geometry property to nil before letting Swift deinitialize it.

    Here is an example of how you may implement it:

    class ViewController: UIViewController {
        @IBOutlet weak var sceneView: SCNView!
        var scene: SCNScene!
    
        // ...
    
        override func viewDidLoad() {
            super.viewDidLoad()
            scene = SCNScene()
            sceneView.scene = scene
    
            // ...
        }
    
        deinit {
            scene.rootNode.cleanup()
        }
    
        // ...
    }
    
    extension SCNNode {
        func cleanup() {
            for child in childNodes {
                child.cleanup()
            }
            geometry = nil
        }
    }
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题