问题描述
我正在构建一个使用Scenekit根据数据库返回的信息显示场景的应用程序。我创建了一个带有类func的自定义类,它创建了一个包含需要绘制的所有内容的SCNNode并将其返回给我的SCNView。在调用此函数之前,我删除任何现有节点。一切都很好,直到我需要第二次调用它。删除旧SCNNode后,在创建新内存之前不会释放内存。有没有标准的方法来删除和替换SCNNode而不会使内存过载?我是iOS开发人员的新手,之前从未真正做过图像。谢谢
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 SCNNode containing everything that needs to be drawn and returns it to my SCNView. Before calling this function I remove any existing nodes. Everything works great until I need to call it a second time. After removing the old SCNNode The memory is not deallocated before creating the new one. Is there a standard way of removing and replacing an SCNNode without overloading memory? I'm new to iOS dev and have never really done graphics before either. Thanks
推荐答案
我遇到了同样的问题,我的SceneKit应用程序泄漏了大量内存。如果将几何
属性设置为 nil $,则会释放
SCNNode
的内存c $ c>在让Swift取消初始化之前。
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
}
}
这篇关于在创建新的SCNNode之前,删除SCNNode不会释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!