【问题标题】:How to use flash to take picture on custom camera?如何使用闪光灯在自定义相机上拍照?
【发布时间】:2015-10-12 13:54:46
【问题描述】:

我到处看了看,但我不知道如何以这种方式切换闪光灯,当我拍照时它会像在 iPhone 相机上那样闪烁 4 次。

我正在使用这个功能来切换闪光灯...

func toggleFlash() {
    let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    if (device.hasTorch) {
        device.lockForConfiguration(nil)
        if (device.torchMode == AVCaptureTorchMode.On) {
            device.torchMode = AVCaptureTorchMode.Off
        } else {
            device.setTorchModeOnWithLevel(1.0, error: nil)
        }
        device.unlockForConfiguration()
    }
}

而且我用这个功能拍照...

func didPressTakePhoto(){
    if let videoConnection = stillImageOutput?.connectionWithMediaType(AVMediaTypeVideo){
        videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
        stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
            (sampleBuffer, error) in

            if sampleBuffer != nil {

                var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                var dataProvider  = CGDataProviderCreateWithCFData(imageData)
                var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault)

                var image:UIImage!

                if self.camera == true {
                    image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.Right)

                } else {
                    image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.LeftMirrored)

                }
                self.tempImageView.image = image
                self.tempImageView.hidden = false
            }


        })
    }


}

【问题讨论】:

  • 我认为这里已经回答了这个问题:stackoverflow.com/a/32301753/4503700
  • @CharlesTruluck 不,我正在努力做到这一点,以便我可以在拍照时加入闪光灯。
  • 谁对不起!我以为你的意思是它不起作用

标签: ios iphone swift camera avcapturesession


【解决方案1】:

这是一个非常混乱的方式。不干净。

func didPressTakePhoto(){

toggleFlash()
sleep(1)
toggleFlash()

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

        if sampleBuffer != nil {

            toggleFlash()

            var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
            var dataProvider  = CGDataProviderCreateWithCFData(imageData)
            var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault)

            var image:UIImage!
            if self.camera == true {
                image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.Right)

            } else {
                image = UIImage(CGImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.LeftMirrored)

            }
            self.tempImageView.image = image
            self.tempImageView.hidden = false
        }


    })
}


}

这很混乱,只是一个想法。 你也可以使用 NSTimer 来触发它两次。

【讨论】:

  • 谢谢!但是时机有点不对劲。而且我一直在摆弄睡眠,但我一直没能把它弄好。我也应该设置什么?
  • 您可以尝试将while i <= 2 更改为while i <= 4。告诉我看起来怎么样。
  • 我去试试!此外,我似乎无法将睡眠设置为双倍。
  • 看来闪存速度很慢。并且这张照片是在闪光灯完成之前拍摄的。
  • 更新了代码。基本上在按下按钮时将其打开,然后在捕获图像时将其关闭。
猜你喜欢
  • 2016-02-26
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多