title: Switch between front and back camera
body: <p>I'm trying to make a custom cameraView, which works so far. However i've reached an issue with switching between front and back camera. I've tried to handle it through a custom enum. However when the <code>switchCamera</code> method is called. it just seem to freeze the camera? How come is that?</p>
  
  <p><strong>Camera Variable</strong></p>
  
  <pre><code>var camera = CameraType.Back
  </code></pre>
  
  <p><strong>viewDidLoad</strong></p>
  
  <pre><code>    switchButton = UIButton(frame: CGRectMake(rightButtonXPoint, 35, 30, 30))
      switchButton.setImage(UIImage(named: "Switch"), forState: UIControlState.Normal)
      switchButton.addTarget(self, action: "switchCamera", forControlEvents: UIControlEvents.TouchUpInside)
      actionView.addSubview(switchButton)
  </code></pre>
  
  <p><strong>ViewWillAppear</strong></p>
  
  <pre><code>override func viewWillAppear(animated: Bool) {
      super.viewWillAppear(animated)
  
      reloadCamera()
  
  
  }
  </code></pre>
  
  <p><strong>SwitchCamera</strong></p>
  
  <pre><code>func switchCamera() {
  
      if camera == CameraType.Back {
          camera = CameraType.Front
      } else {
          camera = CameraType.Back
      }
      reloadCamera()
  }
  </code></pre>
  
  <p><strong>ReloadCamera</strong></p>
  
  <pre><code>func reloadCamera() {
  
      captureSession = AVCaptureSession()
  
  
     // let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
      var captureDevice:AVCaptureDevice! = nil
      if (camera == CameraType.Front) {
          let videoDevices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
  
          for device in videoDevices{
              let device = device as! AVCaptureDevice
              if device.position == AVCaptureDevicePosition.Front {
                  captureDevice = device
                  break
              }
          }
      } else {
          captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
      }
  
      do {
          let input = try? AVCaptureDeviceInput(device: captureDevice)
  
          if (captureSession?.canAddInput(input) != nil){
  
              captureSession?.addInput(input)
  
              stillImageOutput = AVCaptureStillImageOutput()
              stillImageOutput?.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]
  
              if (captureSession?.canAddOutput(stillImageOutput) != nil){
                  captureSession?.addOutput(stillImageOutput)
  
                  previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
  
                  previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
                  previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
                  cameraView.layer.addSublayer(previewLayer!)
                  captureSession?.startRunning()
  
              }
  
              cameraView.bringSubviewToFront(actionView)
              previewImageView.bringSubviewToFront(actionView)
              self.previewImageView.hidden = true
  
  
          }
      }
  
  
  
  
  
  
  
  }
  </code></pre>