如何用图像捕捉时间相机闪光?

时间:2015-10-14 03:10:40

标签: ios iphone xcode swift camera

我想这样做,以便我拍照时我的闪光灯会有时间,但不知道怎么做。我曾经尝试过NSTimer和NSSleep以及其他计时方式,但是因为有时相机需要更长的时间来对焦和拍照,而不是其他时候,它并不总能让闪光灯完全正确。我该如何做到这一点?

以下是我如何做闪光灯...

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

            }


        })
    }


}

1 个答案:

答案 0 :(得分:2)

toggleFlash中,您需要设置

device.flashMode = .On

您正在设置torchMode

纠正此问题应使captureStillImageAsynchronouslyFromConnection期间闪光灯在正确的时间关闭。