LittleyLv
LittleyLv

Reputation: 71

ARKit ARSCNView.snapshot() memory leak

I'm using ARKit. I need to save views in camera into photo album. So I add a button in storyboard, and a function like below:

@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) {
    // ...
}

But my app crashes when I click button many times. So I go to "Debug Navigator" and see the memory rises about 30M once I click the button (like 200M - 235M - 260M ~~~~ 500M+).

What happened? What should I do then?

Upvotes: 0

Views: 1203

Answers (2)

drmatt
drmatt

Reputation: 166

I was experiencing this issue, but found that calling the snapshot method on the main thread solves the memory leak.

Upvotes: 1

ggmorga
ggmorga

Reputation: 61

I am experiencing the same issue in Scenekit (only on iOS12; the same build works fine on iOS11). For now I found a workaround: instead of using the snapshot() method of SCNView I am using the snapshot(atTime:with:antialiasingMode:) method of SCNRenderer. This requires a little extra work for:

  1. creating a renderer object,
  2. setting it's scene property to the scene to be rendered,
  3. aligning the scene pointOfView.

I have replaced my let snapShot = scenView.snapShot

with the following 4 lines (note: I have no animations, so TimeInterval(0) is ok for me) :

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

Upvotes: 2

Related Questions