title: swift photo capture black image
body: <p>I'm trying to set up AVFoundation photo capture using Swift. The preview window works fine but when I press the button to capture the photo, the first picture taken after the app is launched is totally black. Any subsequent pictures I take in the same instance of the app running work correctly, but that first one is always just a black rectangle. The preview layer also flashes black when the picture is taken. Here's the code I'm using for the camera:</p>
  
  <pre><code>import UIKit
  import AVFoundation
  
  class photoCaptureViewController: UIViewController {
  
  let session                     : AVCaptureSession = AVCaptureSession()
  var previewLayer                : AVCaptureVideoPreviewLayer!
  var output = AVCaptureStillImageOutput()
  var capturedImage               : UIImage!
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
    let device = getFrontCamera()
    var error   : NSError? = nil
  
    let input   : AVCaptureDeviceInput? = AVCaptureDeviceInput.deviceInputWithDevice(device, error: &amp;error) as? AVCaptureDeviceInput
  
    if input != nil {
      session.addInput(input)
    }else{
      println(error)
    }
  
    session.sessionPreset = AVCaptureSessionPresetPhoto
  
    previewLayer = AVCaptureVideoPreviewLayer.layerWithSession(session) as! AVCaptureVideoPreviewLayer
    previewLayer.frame = CGRectMake(0, 0, 300, 300)
    previewLayer.setAffineTransform( CGAffineTransformTranslate(CGAffineTransformMakeScale(0.33, 0.33), -375, -480) )
    previewLayer.position = CGPointMake((photoButton.layer.position.x + 70), (photoButton.layer.position.y - 45))
  
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  
    self.view.layer.addSublayer(previewLayer)
  
    self.setPreviewOrientation()
  
    session.startRunning()
  
  }
  
  func setPreviewOrientation() {
    let previewLayerConnection: AVCaptureConnection = self.previewLayer.connection
    let orientation = AVCaptureVideoOrientation(rawValue: UIDevice.currentDevice().orientation.rawValue)
    previewLayerConnection.videoOrientation = orientation!
  }
  
  @IBAction func photoButtonPressed(sender: UIButton)
  {
  
    output.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
  
    if session.canAddOutput(output)
    {
      session.addOutput(output)
    }
  
    var videoConnection = output.connectionWithMediaType(AVMediaTypeVideo)
  
    if videoConnection != nil {
      output.captureStillImageAsynchronouslyFromConnection(output.connectionWithMediaType(AVMediaTypeVideo))
        { (imageDataSampleBuffer, error) -&gt; Void in
          self.capturedImage = UIImage(data: AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer))
  
          UIImageWriteToSavedPhotosAlbum(self.capturedImage, self,nil, nil)
  
      }}
  
  
  }
  
  func getFrontCamera() -&gt; AVCaptureDevice{
    var result : AVCaptureDevice!
    var devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
    for device in devices{
      if (device.position == AVCaptureDevicePosition.Front) {
        result = device as! AVCaptureDevice
      }
    }
    return result
  }
  
  }
  </code></pre>