簡體   English   中英

如何為帶有圖像捕捉的相機閃光燈計時?

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

我想這樣做,以使我的閃光燈在拍攝照片時會與時間同步,但不確定如何拍攝。 我嘗試過使用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

            }


        })
    }


}

toggleFlash ,您需要設置

device.flashMode = .On

您正在設置torchMode

糾正此問題應使flash在captureStillImageAsynchronouslyFromConnection期間的正確時間熄滅。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM