错误线程 1:致命错误:解包可选值时意外发现 nil?

ERROR Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value?

提问人: 提问时间:8/6/2018 更新时间:8/6/2018 访问量:309

问:

是的,我知道有人问过这样的问题,但我已经浏览了所有我能找到的问题,但仍然无法解决这个问题。Maby,我遇到的这个问题也会帮助其他人。

我正在尝试制作一个相机应用程序,但是当我运行该应用程序时,它立即失败。我已经标记了它失败的地方,并给了我以下警告......

线程 1:致命错误:解开可选值时意外发现 nil

这是代码,我已经标记了收到警告的部分:

    import UIKit
import AVFoundation

class ViewController: UIViewController {

    var captureSession = AVCaptureSession()

    var backCamera: AVCaptureDevice?
    var frontCamera: AVCaptureDevice?
    var currentCamera: AVCaptureDevice?

    var photoOutput: AVCapturePhotoOutput?

    var cameraPreviewlayer: AVCaptureVideoPreviewLayer?

    override func viewDidLoad() {
        super.viewDidLoad()

        setupCaptureSession()
        setupInputOutput()
        setupPreviewLayer()
        startRunningCaptureSession()

        // Do any additional setup after loading the view, typically from a nib.
    }

    func setupCaptureSession() {
        captureSession.sessionPreset = AVCaptureSession.Preset.photo
    }

    func setupDevice() {
        let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)

        let devices = deviceDiscoverySession.devices

        for device in devices {
            if device.position == AVCaptureDevice.Position.back {
                backCamera = device
            } else if device.position == AVCaptureDevice.Position.front {
                frontCamera = device
            }
        }

        currentCamera = backCamera
    }

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

        } catch {
            print(error)
        }
    }

    func setupPreviewLayer(){
        cameraPreviewlayer = AVCaptureVideoPreviewLayer(session: captureSession)
        cameraPreviewlayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
        cameraPreviewlayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
        cameraPreviewlayer?.frame = self.view.frame
        self.view.layer.insertSublayer(cameraPreviewlayer!, at: 0)
    }

    func startRunningCaptureSession() {
        captureSession.startRunning()
    }

    @IBAction func CameraButton_TouchUpInside(_ sender: Any) {

        performSegue(withIdentifier: "showPhoto_Segue", sender: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
iOS iPhone Swift 警告

评论


答:

0赞 Zdeněk Topič 8/6/2018 #1

您没有在代码中的任何位置调用,即使调用了,您也始终设置为如果设备没有后置摄像头,则可能是哪个。setupDevice()currentCamerabackCameranil

只需将呼叫添加到您的:setupDevice()viewDidLoad()

    override func viewDidLoad() {
        super.viewDidLoad()

        setupCaptureSession()
        setupDevice()
        setupInputOutput()
        setupPreviewLayer()
        startRunningCaptureSession()
    }
0赞 Alexandre Gaubil 8/6/2018 #2

在调用 之前,您应该调用 ,以将值赋值给 如下所示:setupDevice()override func viewDidLoad()setupInputOutput()currentCamera

override func viewDidLoad() {
    super.viewDidLoad()

    setupCaptureSession()
    setupDevice()
    setupInputOutput()
    setupPreviewLayer()
    startRunningCaptureSession()

}