让闪光灯与AVFoundation相机配合使用

tah*_*oop 1 camera avfoundation ios swift

我使用AVFoundation创建了一个相机.我想将闪存设置为自动,因此用户无需处理它.我尝试添加代码backCamera.flashMode = AVCaptureFlashMode.Auto,它似乎没有工作.非常感谢您提供的任何反馈!

     func setUpCamera() {

              captureSession = AVCaptureSession()
              captureSession!.sessionPreset = AVCaptureSessionPresetPhoto

              let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

    // CODE THAT SEEMS LIKE IT SHOULD WORK
                    if backCamera.hasFlash {

                       backCamera.flashMode = AVCaptureFlashMode.Auto
                    }
              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) {

                 captureSession!.addInput(input)

                 stillImageOutput = AVCaptureStillImageOutput()
                 stillImageOutput!.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]

                 if captureSession!.canAddOutput(stillImageOutput) {

                    captureSession!.addOutput(stillImageOutput)
                    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                    previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
                    previewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.Portrait
                    previewVideoView.layer.addSublayer(previewLayer!)
                    captureSession!.startRunning()
                 }
              }
           }

   @IBAction func onSnapPhotoButtonPressed(sender: UIButton) {

      if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {

         videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
         stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(sampleBuffer, error) in

            if (sampleBuffer != nil) {

               let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
               let dataProvider = CGDataProviderCreateWithCFData(imageData)
               let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault)

               let image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)
               self.imageView.image = image

               self.clearPhotoButton.hidden = false
               self.nextButton.hidden = false
               self.view.bringSubviewToFront(self.imageView)
            }
         })
      }
   }
Run Code Online (Sandbox Code Playgroud)

mat*_*att 5

来自文档:

在尝试设置捕获设备的属性(其聚焦模式,曝光模式等)之前,必须首先使用该lockForConfiguration:方法获取设备上的锁定.然后,您可以使用该unlockForConfiguration方法设置属性并释放锁定.

我不认为你这样做.