2015-10-14 95 views
1

我想让我的闪光灯在我拍照时能与时间同步,但不知道如何。我尝试过使用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 

      } 


     }) 
    } 


} 

回答

2

toggleFlash,你需要设置

device.flashMode = .On 

您正在设置torchMode

修正此问题可能会使闪光灯在captureStillImageAsynchronouslyFromConnection期间的正确时间熄灭。

+0

我需要只调用一次该方法吗?如果是的话,我应该在哪里打电话? –

+0

在配置期间设置'flashMode',在那里你现在正在设置'torchMode' –

+0

这就像一个魅力!谢谢。 (: –