简体   繁体   中英

AVFoundation camera crashing while switching to front camera (refreshing camera)

I am a new bee to iOS, working on simple application with swift, In that I need a custom camera. I am using AVFoundation but the app is showing black screen for long time, then it's loading the camera. here is my code

 func prepareCamera() {
    captureSession.sessionPreset = AVCaptureSessionPresetPhoto
    if frontCam{
    if let availableDevices = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .front).devices {
        captureDevice = availableDevices.first
        DispatchQueue(label: "prepare").async {
        self.beginSession()
        }
    }
    }else{
        if let availableDevices = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .back).devices {
            captureDevice = availableDevices.first
            beginSession()
        }
    }
}
@IBAction func switchCameraBtnClicked(_ sender: Any) {
    frontCam = !frontCam
    prepareCamera()
}
func beginSession () {

    do {
        let captureDeviceInput = try AVCaptureDeviceInput(device: captureDevice)
        if let inputs = captureSession.inputs as? [AVCaptureDeviceInput] {
            for input in inputs {
                captureSession.removeInput(input)
            }
        }
        if captureSession.inputs.isEmpty {
     captureSession.addInput(captureDeviceInput)
        }

    }catch {
        print(error.localizedDescription)
    }


    if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) {
        self.previewLayer = previewLayer
      //  self.view.layer.addSublayer(self.previewLayer)
        self.view.layer.insertSublayer(self.previewLayer, at: 0)
        self.previewLayer.frame = self.view.layer.frame

        captureSession.startRunning()

        let dataOutput = AVCaptureVideoDataOutput()
        dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString):NSNumber(value:kCVPixelFormatType_32BGRA)]

        dataOutput.alwaysDiscardsLateVideoFrames = true

        if captureSession.canAddOutput(dataOutput) {
            captureSession.addOutput(dataOutput)
        }

        captureSession.commitConfiguration()


        let queue = DispatchQueue(label: "com.graymatics.customcamera")
        dataOutput.setSampleBufferDelegate(self, queue: queue)



    }

}

Please correct me if the code is not proper.

Finally found the solution

here is my code:

override func viewDidDisappear(_ animated: Bool) {
         self.stopCaptureSession()
    }


 func stopCaptureSession () {
        self.captureSession.stopRunning()

        if let inputs = captureSession.inputs as? [AVCaptureDeviceInput] {
            for input in inputs {
                self.captureSession.removeInput(input)
            }
        }        
    }

session needs to be stopped while moving on from the current view controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM