2015-10-20 67 views
0

我正在用相機的自定義視圖構建自定義相機應用程序。在ViewWillAppear部分中添加以下代碼,我在我添加星星的區域中出現以下錯誤: 二元運算符'!='不能應用於類型爲'Bool'和'NilLiteralConvertible'的操作數在swift 2.0中構建自定義相機應用程序時的錯誤

任何幫助非常感謝。

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() 

       } 


      } 

回答

0

captureSession.canAddInput(input)返回Bool,所以沒有必要檢查!= nil。你的if語句可以成爲:

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

此外,您還宣佈inputAVCaptureDeviceInput!!意味着它不應該是零,但如果出現錯誤,您將其指定爲零。如果發生錯誤,這將會崩潰。如有必要,您應聲明inputAVCaptureDeviceInput?並展開input

相關問題