SceneKit内存泄漏

时间:2019-07-06 15:25:03

标签: ios swift memory scenekit scnscene

使用VC隔离到scnView后,应用程序的内存增加了。我使用了deinit并将geometry设置为nil,但这没有帮助。我在堆栈上看到了一些有关使用deinit解决此问题的技巧,但对我而言不起作用。每次使用scnScene SceneKit: too much memory persisting

返回此ViewController时,内存都会增加
var ship1: SCNNode!
    var ship2: SCNNode!
    var ship3: SCNNode!
    var ship4: SCNNode!
    let cameraNode = SCNNode()
    let lightNode = SCNNode()
    let ambientLightNode = SCNNode()

 class RecordVideoViewControllerWS: UIViewController {


    @IBOutlet weak var scnView: SCNView!

     override func viewDidLoad() {
            super.viewDidLoad()
          cameraNode.camera = SCNCamera()
                scnScene.rootNode.addChildNode(cameraNode)
                cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
                scnView.scene = scnScene

                lightNode.light = SCNLight()
                lightNode.light!.type = .omni
                lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
                scnScene.rootNode.addChildNode(lightNode)

                // create and add an ambient light to the scene
                ambientLightNode.light = SCNLight()
                ambientLightNode.light!.type = .ambient
                ambientLightNode.light!.color = UIColor.darkGray
                scnScene.rootNode.addChildNode(ambientLightNode)
                 ship1 = nodeFromResource(assetName: "shipFolder/test0", extensionName: "scn")
                ship2 = nodeFromResource(assetName: "shipFolder/test1", extensionName: "scn")
                ship3 = nodeFromResource(assetName: "shipFolder/test2", extensionName: "scn")
                ship4 = nodeFromResource(assetName: "shipFolder/test3", extensionName: "scn")


                scnScene.rootNode.addChildNode(ship1)
                scnScene.rootNode.addChildNode(ship2)
                scnScene.rootNode.addChildNode(ship3)
                scnScene.rootNode.addChildNode(ship4)
    }



    override func viewWillDisappear(_ animated: Bool) {

                ship1.removeFromParentNode()
                ship1.geometry = nil

                ship2.removeFromParentNode()

                ship2.geometry = nil

                ship3.removeFromParentNode()

                ship3.geometry = nil

                ship4.removeFromParentNode()

                ship4.geometry = nil

                cameraNode.removeFromParentNode()
                cameraNode.geometry = nil
                lightNode.removeFromParentNode()
                lightNode.geometry = nil

                ambientLightNode.removeFromParentNode()
                ambientLightNode.geometry = nil



                }
 deinit {
        scnScene.rootNode.cleanup()
    }


}

    extension SCNNode {
        func cleanup() {
            for child in childNodes {
                child.cleanup()
            }
            geometry = nil
        }
    }

0 个答案:

没有答案