在swift 2.0中构建自定义相机应用程序时出错

时间:2015-10-20 15:06:58

标签: ios swift camera swift2

我正在构建一个自定义相机应用程序,其中包含相机的自定义视图。在ViewWillAppear部分中添加以下代码时,我在添加星星的区域中收到以下错误: 二进制运算符'!='不能应用于'Bool'和'NilLiteralConvertible'类型的操作数

非常感谢任何帮助。

captureSession = AVCaptureSession()
        captureSession.sessionPreset = AVCaptureSessionPreset1920x1080

        let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

        var error : NSError?
        var input: AVCaptureDeviceInput!

        do {
            input = try AVCaptureDeviceInput(device: backCamera)
        } catch let error1 as NSError {
            error = error1
            input = nil
        }

if error == nil && captureSession.canAddInput(input) != nil { *********

                captureSession.addInput(input)

                stillImageOutPut = AVCaptureStillImageOutput()
                stillImageOutPut.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]

                if (captureSession.canAddOutput(stillImageOutPut) != nil){
                    captureSession.addOutput(stillImageOutPut)

                    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                    previewLayer.videoGravity = AVLayerVideoGravityResizeAspect
                    previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
                    cameraView.layer.addSublayer(previewLayer)
                    captureSession.startRunning()

                }


            }

1 个答案:

答案 0 :(得分:0)

captureSession.canAddInput(input)会返回Bool,因此无需检查!= nil。您的if语句可以变为:

if error == nil && captureSession.canAddInput(input) {

另外,您将input声明为AVCaptureDeviceInput!!意味着它不应该是nil,但如果有错误则指定为nil。如果出现错误,这将崩溃。您应该将input声明为AVCaptureDeviceInput?并在必要时解开input