title: Capture current camera image using AVFoundation
body: <p>I'm trying to capture the image and save it to a variable when I press "myButton". What should I do?</p>
  
  <p>My code is as follows:</p>
  
  <pre><code>import UIKit
  import AVFoundation
  import MobileCoreServices
  
  class ViewController: UIViewController {
  
      let captureSession = AVCaptureSession()
      var previewLayer : AVCaptureVideoPreviewLayer?
      var captureDevice : AVCaptureDevice?
      @IBOutlet var myTap: UITapGestureRecognizer!
      @IBOutlet weak var myButton: UIButton!
  
      @IBAction func shotPress(sender: UIButton) {
          //Save image to variable somehow
          })
          var stillImageOutput = AVCaptureStillImageOutput()
          stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
  
          if captureSession.canAddOutput(stillImageOutput) {
              captureSession.addOutput(stillImageOutput)
          }
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
          captureSession.sessionPreset = AVCaptureSessionPresetHigh
          let devices = AVCaptureDevice.devices()
          for device in devices {
              if (device.hasMediaType(AVMediaTypeVideo)) {
                  if(device.position == AVCaptureDevicePosition.Back) {
                      captureDevice = device as? AVCaptureDevice
                      if captureDevice != nil {
                          beginSession()
                      }
                  }
              }
          }
      }
  
      func updateDeviceSettings(focusValue : Float, isoValue : Float) {
          if let device = captureDevice {
              if(device.lockForConfiguration(nil)) {
                  device.focusMode = AVCaptureFocusMode.ContinuousAutoFocus
                  device.unlockForConfiguration()
              }
          }
      }
  
      func beginSession() {
          var err : NSError? = nil
          captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &amp;err))
          if err != nil {
              println("error: \(err?.localizedDescription)")
          }
          previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
          self.view.layer.addSublayer(previewLayer)
          self.view.bringSubviewToFront(myButton)
          previewLayer?.frame = self.view.layer.frame
          captureSession.startRunning()
      }
  
  }
  </code></pre>