2

I have an ARKit application where I need to get the capturedImage of the current ARFrame object and use it for further processing. My code looks roughly like this:

guard let frame = sceneView.session.currentFrame else {
  return
}

someMethod(frame)

Now, I expect the frame to be nil only during the first few miliseconds of the application where ARKit is being set up, or if the camera permission is denied for this application. However, when the application is first installed and I deny permission when asked, I get non-nil frames where every pixel is just black. If I run the application again, I get nil frames as I should. Is this a bug or is there a logic behind this?

halileohalilei
  • 2,220
  • 2
  • 25
  • 52

1 Answers1

0

My guess was code for handling the permission runs asynchronously and we get some non-nil frames before it is completed.
So i tried this inside viewDidLoad:

AVCaptureDevice.requestAccess(for: .video) { (permission) in
    if !permission{
        print("not permited")
    }else{
        print("permited")
        self.scan = true
    }
}

and

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
    if scan{
        guard let frame = sceneView.session.currentFrame else {
            return
        }
        //code
    }
}

Here you let your code run only if permission is granted by user.

Alok Subedi
  • 1,601
  • 14
  • 26
  • While this could solve the situation at hand, I still don't know why I'm getting non-nil frames. Also, I'm not asking for the permission explicitly in my app. The permission dialog pops up when the `ARSCNView` is added to the controller, which is on start up. – halileohalilei Jan 08 '18 at 19:59
  • I tried your code and found that we get non nil frames when dialog pops up till we deny permission. And even after denying we get about 25 non nil frames. So my conclusion is permission part runs asynchronously. – Alok Subedi Jan 09 '18 at 02:03
  • https://stackoverflow.com/a/26072092/5567142 Updating ui should be done in dispatch async means it does run asynchronously – Alok Subedi Jan 09 '18 at 02:16