应用程序崩溃:在调用beginConfiguration和commitConfiguration之间可能不会调用startRunning?

时间:2018-09-05 00:34:13

标签: swift xcode avcapturesession

我正在开发一个使用名为WeScan的pod的应用程序(请参见其github)。

设置完所有内容并将正确的项目添加到我的info.plist文件后,我的应用程序不断崩溃。

错误是:

  

由于未捕获的异常'NSGenericException'而终止应用程序,原因:'***-[AVCaptureSession startRunning] startRunning可能在调用beginConfiguration和commitConfiguration之间未调用”“

以下是我认为导致问题的Pod代码:

init?(videoPreviewLayer: AVCaptureVideoPreviewLayer) {
self.videoPreviewLayer = videoPreviewLayer
super.init()

captureSession.beginConfiguration()
captureSession.sessionPreset = AVCaptureSession.Preset.photo

photoOutput.isHighResolutionCaptureEnabled = true

let videoOutput = AVCaptureVideoDataOutput()
videoOutput.alwaysDiscardsLateVideoFrames = true

guard let inputDevice = AVCaptureDevice.default(for: AVMediaType.video),
    let deviceInput = try? AVCaptureDeviceInput(device: inputDevice),
    captureSession.canAddInput(deviceInput),
    captureSession.canAddOutput(photoOutput),
    captureSession.canAddOutput(videoOutput) else {
        let error = ImageScannerControllerError.inputDevice
        delegate?.captureSessionManager(self, didFailWithError: error)
        return
}

captureSession.addInput(deviceInput)
captureSession.addOutput(photoOutput)
captureSession.addOutput(videoOutput)

videoPreviewLayer.session = captureSession
videoPreviewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "video_ouput_queue"))

captureSession.commitConfiguration()
}

// MARK: Capture Session Life Cycle

/// Starts the camera and detecting quadrilaterals.
internal func start() {
    let authorizationStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)

    switch authorizationStatus {
    case .authorized:
        self.captureSession.startRunning()
        isDetecting = true
    case .notDetermined:
        AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: { (_) in
           DispatchQueue.main.async { [weak self] in
                self?.start()
            }
        })
    default:
        let error = ImageScannerControllerError.authorization
        delegate?.captureSessionManager(self, didFailWithError: error)
    }
}

internal func stop() {
    captureSession.stopRunning()
}

internal func capturePhoto() {
    let photoSettings = AVCapturePhotoSettings()
    photoSettings.isHighResolutionPhotoEnabled = true
    photoSettings.isAutoStillImageStabilizationEnabled = true

    if let photoOutputConnection = self.photoOutput.connection(with: .video) {
        photoOutputConnection.videoOrientation = AVCaptureVideoOrientation(deviceOrientation: UIDevice.current.orientation) ?? AVCaptureVideoOrientation.portrait
    }

2 个答案:

答案 0 :(得分:0)

尝试放置语句captureSession.commitConfiguration() 在守卫区外面。这是因为提交必须与beginConfiguration()

在同一级别完成

答案 1 :(得分:0)

我最近遇到了这个问题:当我在应用程序的一个完全不同的部分工作时,它突然发生了。

删除并重新安装似乎已经解决了。