1

VC转到with后,应用程序的内存增加了scnView。我已经使用deinit并设置geometry为,nil但它没有帮助。我在堆栈上看到了一些关于使用deinit来解决这个问题的提示,但它对我不起作用。每次我用scnScene SceneKit 回到这个 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
        }
    }
4

0 回答 0