添加AVCaptureDeviceInput时AVCaptureSession崩溃

时间:2016-06-03 05:14:35

标签: ios swift avfoundation avcapturesession

在设置相机会话时,我在Crashlytics上显示了一个奇怪的崩溃。 堆栈跟踪显示崩溃发生在addInput方法中。

func setupCamSession(){
    self.captureSession = AVCaptureSession()
    self.cameraView.setSession(self.captureSession)
    self.sessionQueue = dispatch_queue_create("com.myapp.camera_queue", DISPATCH_QUEUE_SERIAL)
    self.setupResult = .Success
    switch AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo){
    case .Authorized:
        break //already we set it to success
    case .NotDetermined:
        // The user has not yet been presented with the option to grant video access.
        // We suspend the session queue to delay session setup until the access request has completed 
        dispatch_suspend(self.sessionQueue)
        AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (granted) -> Void in
            if ( !granted ) {
                self.setupResult = .CameraNotAuthorized
            }
            dispatch_resume(self.sessionQueue)
        })
    default:
        self.setupResult = .CameraNotAuthorized
    }

    dispatch_async(self.sessionQueue){
        if self.setupResult != .Success{
            return
        }
        //link input to captureSession
        guard let videoDevice = self.deviceWithMediaType(AVMediaTypeVideo, position: AVCaptureDevicePosition.Back) else{
            AppLog("Video Device Unavailable")
            self.setupResult = .SessionConfigurationFailed
            return
        }
        var videoDeviceInput: AVCaptureDeviceInput!
        do {
            videoDeviceInput = try AVCaptureDeviceInput(device: videoDevice)
        }catch {
            AppLog("Could not create video device input")
        }

        /////////////////////////////////////////////////////
        self.captureSession.beginConfiguration()

        if self.captureSession.canAddInput(videoDeviceInput){
            self.captureSession.addInput(videoDeviceInput)
            self.videoDeviceInput = videoDeviceInput
            self.videoDevice = videoDevice
            dispatch_async(dispatch_get_main_queue()){
                //update the cameraView layer on the main thread
                let previewLayer : AVCaptureVideoPreviewLayer = self.cameraView.layer as! AVCaptureVideoPreviewLayer
                previewLayer.connection.videoOrientation = AVCaptureVideoOrientation(ui:UIApplication.sharedApplication().statusBarOrientation)
            }
        }else{
            AppLog("Could not add video device input to the session")
            self.setupResult = .SessionConfigurationFailed
        }

        //link output to captureSession
        let stillImageOutput = AVCaptureStillImageOutput()
        if self.captureSession.canAddOutput(stillImageOutput){
            self.captureSession.addOutput(stillImageOutput)
            self.stillImageOutput = stillImageOutput
            stillImageOutput.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]
        }else{
            AppLog("Could not add still image output to the session")
            self.setupResult = .SessionConfigurationFailed
        }

        self.captureSession.commitConfiguration()
        /////////////////////////////////////////////////////
    }
}

func runSession(){
    dispatch_async(self.sessionQueue){
        switch self.setupResult!{
        case .Success:
            self.videoDeviceInput!.device.addObserver(self, forKeyPath: "adjustingFocus", options: NSKeyValueObservingOptions.New, context: nil)
            self.captureSession.addObserver(self, forKeyPath: "running", options: [.New], context: &SessionRunningContext)
            self.captureSession.startRunning()
            self.captureSessionRunning = self.captureSession.running
            if !self.captureSessionRunning {
                self.captureSession.removeObserver(self, forKeyPath: "running", context: &SessionRunningContext)
                self.videoDeviceInput?.device?.removeObserver(self, forKeyPath: "adjustingFocus", context: nil)
            }
       default:
       //Handle errors.
       }
  }
func stopCaptureSession(){
    dispatch_async(self.sessionQueue){
        if self.setupResult == .Success{
            if self.captureSessionRunning{
                self.captureSession.stopRunning()
                self.videoDeviceInput?.device?.removeObserver(self, forKeyPath: "adjustingFocus", context: nil)
                self.captureSession.removeObserver(self, forKeyPath: "running", context: &SessionRunningContext)
            }
            self.captureSessionRunning = false
        }
    }
}

在viewDidLoad中调用setupCamSession,在viewWillAppear中调用runSession,并且我在viewWillDisappear中也有一个stopSession方法。与摄像机会话相关的所有内容都将在后台串行队列中调度。

崩溃不会100%发生,我无法在我使用的设备上重现崩溃。 谢谢  This is a screenshot of the stacktrace

2 个答案:

答案 0 :(得分:1)

确保您要移除deinit上的观察者。当我回到相机捕捉屏幕时我看到了这种情况,我没有移除adjustingFocus的观察者。我在deinit删除后一切都很顺利。

答案 1 :(得分:1)

有同样的问题。在Info.plist文件中添加Privacy - Camera Usage Description的使用说明后,它已得到解决。这个答案包含有关如何设置描述的提示:

Request Permission for Camera and Library in iOS 10 - Info.plist