线程1:致命错误:展开一个可选值(setupInputOutput)时意外发现nil

时间:2019-04-16 12:12:03

标签: swift

我正在尝试向我的应用程序添加摄像头功能,但是这些消息一直显示在屏幕上,因此我无法为其找到解决方案。有什么可以代替的例子吗?

   func setupInputOutput(){
    do {
        let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!);captureSession.addInput(captureDeviceInput)
        photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format:[AVVideoCodecKey:  AVVideoCodecType.jpeg])], completionHandler: nil)

       } catch {print(error)

1 个答案:

答案 0 :(得分:0)

模拟器没有您需要在真实设备中运行的视频输入

func setupInputOutput() {  

   do { 
       guard let currentCamera = AVCaptureDevice.default(for: AVMediaType.video) else { return }
       let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera);captureSession.addInput(captureDeviceInput)
       photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format:[AVVideoCodecKey:  AVVideoCodecType.jpeg])], completionHandler: nil)

   } catch {    
     print(error)

   } 

}