imaginary-cloud / CameraManager

Simple Swift class to provide all the configurations you need to create custom camera view in your app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Opening and closing camera quickly will randomly cause issues

kselvin opened this issue · comments

I try to call

cameraManager.stopAndRemoveCaptureSession whenever i close camera, but it doesn't seem to work i guess quick enough?

When i open and close the camera a lot i get 1 of 2 issues either

Terminating app due to uncaught exception 'NSGenericException', reason: '*** -[AVCaptureSession startRunning] startRunning may not be called between calls to beginConfiguration and commitConfiguration'

on this function

fileprivate func setupCamera( completion: @escaping () -> Void) {
captureSession = AVCaptureSession()

    sessionQueue.async(execute: {
        if let validCaptureSession = self.captureSession {
            validCaptureSession.beginConfiguration()
            validCaptureSession.sessionPreset = AVCaptureSession.Preset.high
            self._updateCameraDevice(self.cameraDevice)
            self._setupOutputs()
            self._setupOutputMode(self.cameraOutputMode, oldCameraOutputMode: nil)
            self._setupPreviewLayer()
            self._updateIlluminationMode(self.flashMode)
            self._updateCameraQualityMode(self.cameraOutputQuality)
            
            DispatchQueue.global().sync(execute: {
                validCaptureSession.commitConfiguration()
            })
           // validCaptureSession.commitConfiguration()

            validCaptureSession.startRunning()
            self._startFollowingDeviceOrientation()
            self.cameraIsSetup = true
            self._orientationChanged()
            
            completion()
        }
    })
}

OR I'll get an error about how you cannot add a preview session to more than one view at a time

Any help would be greatly appreciated!

Thanks in advance

  • Keith

Note : This almost NEVER happens on iPhone X. Only on the older phones that load more slowly

My best solution to the issue was keeping one instance of cameraManager as global variable and pausing and resuming capture session when needed. I'm guessing this isn't the best solution but at least the app isn't crashing!

@kselvin actually that's the preferred way of using cameraManager, as it says on the README