【问题标题】:Crash in AVCaptureSession when adding an AVCaptureDeviceInput添加 AVCaptureDeviceInput 时 AVCaptureSession 崩溃
【发布时间】:2016-10-03 01:10:27
【问题描述】:

在设置相机会话时,我在 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
        }
    }
}

setupCamSession 在 vi​​ewDidLoad 中调用,runSession 在 vi​​ewWillAppear 中调用,我在 viewWillDisappear 中还有一个 stopSession 方法。与相机会话相关的所有内容都在后台串行队列中调度。

崩溃并非 100% 发生,我无法在我使用的设备上重现崩溃。 谢谢

【问题讨论】:

  • 将其移至 viewWillAppear 或 viewDidAppear
  • 为什么?我更喜欢将它保存在 viewDidLoad() 中。事实上,主屏幕有摄像头视图,我们在主屏幕上多次推送和弹出视图控制器。因此 ViewWillAppear 将被多次调用,并且每次都会再次设置相机会话。目前它只运行相机会话。
  • 我的灵感来自developer.apple.com/library/ios/samplecode/AVCam/Listings/…。他们在 viewDidLoad() 中设置输入
  • 崩溃报告中还有更多信息吗?就像“在参数寄存器中找到的选择器名称”?
  • 没有。这就是我在报告中的全部内容。

标签: ios swift avfoundation avcapturesession


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    确保您正在删除 deinit 上的观察者。我在返回相机捕获屏幕时看到了这种情况,并且我没有移除 adjustingFocus 的观察者。一旦我在deinit 中删除了它,一切都很好。

    【讨论】:

    • 我编辑了我的问题并添加了 stopCaptureSession() 方法。这个方法是为了调整焦点而移除观察者。日志说观察者已被删除,然后 deinit 已被调用。
    • videoDeviceInput.device 是否有可能以某种方式变为 nil?如果发生这种情况,self.videoDeviceInput?.device?.removeObserver(self, forKeyPath: "adjustingFocus", context: nil) 将不会被调用。
    • 非常感谢。这正是我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多