简体   繁体   English

如何为带有图像捕捉的相机闪光灯计时?

[英]How to time camera flash with image capture?

I want to make it so that my flash will time with when I take a picture, but not sure how. 我想这样做,以使我的闪光灯在拍摄照片时会与时间同步,但不确定如何拍摄。 I've tried to do a NSTimer and NSSleep and other ways of timing it, but because sometimes the camera takes longer to focus and take the picture, than other times, it doesn't always get the flash exactly right. 我尝试过使用NSTimer和NSSleep以及其他方式对其进行计时,但是由于有时相机对焦和拍照所需的时间比其他时间更长,因此它并不总是能使闪光灯完全正确。 How would I accomplish this? 我将如何完成?

Here is how I do the flash... 这是我做闪光灯的方法...

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

And here is how I take the picture... 这就是我的拍照方式...

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

            }


        })
    }


}

In toggleFlash , you need to set toggleFlash ,您需要设置

device.flashMode = .On

You're setting the torchMode . 您正在设置torchMode

Correcting this should make the flash go off at the right time during captureStillImageAsynchronouslyFromConnection . 纠正此问题应使flash在captureStillImageAsynchronouslyFromConnection期间的正确时间熄灭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM