title: How to time camera flash with image capture?
body: <p>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. How would I accomplish this?</p>
  
  <p>Here is how I do the flash... </p>
  
  <pre><code>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()
      }
  }
  </code></pre>
  
  <p>And here is how I take the picture...</p>
  
  <pre><code>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
  
              }
  
  
          })
      }
  
  
  }
  </code></pre>