2017-10-21 99 views
0

我試圖實現一些類似於snapchat的自定義相機。我不明白爲什麼我的圖像在賽季期間總是爲零。也許一雙新鮮的眼睛可能會有所幫助,因爲我一直在爲此工作2天...AVCapturePhoto爲什麼我的照片總是無?

當我點擊拍照按鈕(運行「livePhotoTapped」)時,應用程序崩潰,錯誤:致命錯誤:意外地發現零而展開的可選值,指的是圖像是零

任何幫助將是不錯:)

@IBOutlet weak var cameraView: UIView! 

//session to capture data 
var captureSession = AVCaptureSession() 
//which camera to use 
var backFacingCamera: AVCaptureDevice? 
var frontFacingCamera: AVCaptureDevice? 
var currentDevice: AVCaptureDevice? 

var stillImageOutput: AVCaptureStillImageOutput? 
var stillImage: UIImage? 

//camera preview layer 
var cameraPreviewLayer: AVCaptureVideoPreviewLayer? 


func setupCaptureSessionCamera() { 
    //this makes sure to get full res of camera 
    captureSession.sessionPreset = AVCaptureSession.Preset.photo 

    var devices = AVCaptureDevice.devices(for: .video) 

    //query available devices 
    for device in devices { 

     if device.position == .front { 
      frontFacingCamera = device 
     } else if device.position == .back { 
      backFacingCamera = device 
     } 
    }//end iteration 

    //set a default device 
    currentDevice = backFacingCamera 

    //configure session w output for capturing still img 
    stillImageOutput = AVCaptureStillImageOutput() 
    stillImageOutput?.outputSettings = [AVVideoCodecKey : AVVideoCodecType.jpeg] 


    do { 
     //capture the data from whichevr camera we r using..imagine as buffer to translate data into real world image 
     let captureDeviceInput = try AVCaptureDeviceInput(device: currentDevice!) 

     captureSession.addInput(captureDeviceInput) 
     captureSession.addOutput(stillImageOutput!) 

     //setup camera preview layer 
     cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 

     //add the preview to our specified view in the UI 
     view.layer.addSublayer(cameraPreviewLayer!) 
     cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill 
     cameraPreviewLayer?.frame = cameraView.frame 

     captureSession.startRunning() 

    } catch let error { 

     print(error) 

    }//end do 
} 

    @IBAction func livePhotoTapped(_ sender: UIButton) { 

    let videoConnection = stillImageOutput?.connection(with: .video) 

    //capture still image async 
    stillImageOutput?.captureStillImageAsynchronously(from: videoConnection!, completionHandler: { (imageDataBuffer, error) in 

     if let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: imageDataBuffer!, previewPhotoSampleBuffer: imageDataBuffer!) { 

      self.stillImage = UIImage(data: imageData) 
      self.performSegue(withIdentifier: "toPreviewPhoto", sender: self) 
     } 

    }) 
} 
+0

你的'imageDataBuffer'是nil嗎?在'if let imageData = ...'之前加'print(imageDataBuffer)'並看看它是否爲 – 3stud1ant3

+0

你在哪裏調用'setupCaptureSessionCamera'? –

回答

0

像這樣使用:

對於CMSampleBufferIsValid檢查這個link

@IBAction func livePhotoTapped(_ sender: UIButton) { 

    let videoConnection = stillImageOutput?.connection(with: .video) 

    //capture still image async 
    stillImageOutput?.captureStillImageAsynchronously(from: videoConnection!, completionHandler: { (imageDataBuffer, error) in 

     if error != nil { 
      print("error \(error)") 
     } else { 
      if let imageBuffer = imageDataBuffer, CMSampleBufferIsValid(imageBuffer) { 
       if let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: imageDataBuffer!, previewPhotoSampleBuffer: imageDataBuffer!) { 

        self.stillImage = UIImage(data: imageData) 
        self.performSegue(withIdentifier: "toPreviewPhoto", sender: self) 
       }  
      } else { 
       print("imageDataBuffer is nil or not valid") 
      } 
     } 
    }) 
} 
0

您正在強制展開一個零對象。

var currentDevice: AVCaptureDevice? 

看起來這種方法已被棄用:

+ (NSArray<AVCaptureDevice *> *)devices NS_DEPRECATED(10_7, NA, 4_0, 10_0, "Use AVCaptureDeviceDiscoverySession instead."); 

您是否嘗試過 「AVCaptureDeviceDiscoverySession」?

相關問題