gpt4 book ai didi

swift - ARKit ARSCNView.snapshot() 内存泄漏

转载 作者:行者123 更新时间:2023-11-28 11:54:09 24 4
gpt4 key购买 nike

我正在使用 ARKit。我需要将相机中的 View 保存到相册中。所以我在 Storyboard 中添加了一个按钮,以及如下所示的功能:

@IBAction func saveScreenshot() {
let snapShot = self.sceneView.snapshot()
UIImageWriteToSavedPhotosAlbum(snapShot, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}

@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
// ...
}

但是当我多次点击按钮时,我的应用程序崩溃了。因此,我转到“Debug Navigator”并看到单击按钮后内存增加了大约 30M(例如 200M - 235M - 260M ~~~~ 500M+)。

发生了什么事?那我该怎么办呢?

最佳答案

我在 Scenekit 中遇到了同样的问题(仅在 iOS12 上;相同的构建在 iOS11 上运行良好)。现在我找到了一个解决方法:我没有使用 SCNView 的 snapshot() 方法,而是使用 SCNRenderer 的 snapshot(atTime:with:antialiasingMode:) 方法。这需要一些额外的工作:

  1. 创建渲染器对象,
  2. 将它的场景属性设置为要渲染的场景,
  3. 对齐场景 pointOfView。

我已经更换了我的让 snapShot = scenView.snapShot

以下 4 行(注意:我没有动画,所以 TimeInterval(0) 对我来说没问题):

let renderer = SCNRenderer(device: MTLCreateSystemDefaultDevice(), options: [:])
renderer.scene = 场景
renderer.pointOfView = sceneView.pointOfView
让 snapShot = renderer.snapshot(atTime: TimeInterval(0), with: size, antialiasingMode: .none)

关于swift - ARKit ARSCNView.snapshot() 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51397382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com