title: How do I implement camera changing from front to back camera
body: <p>Below is the code for the camera section, I tried adding in a boolean to detect when the front camera is activated but I receive an error.  </p>
  
  <pre><code>import UIKit
  import AVFoundation 
  class MainCameraCollectionViewCell: UICollectionViewCell {
      @IBOutlet weak var myView: UIView!
      var captureSession = AVCaptureSession()
      var backCamera: AVCaptureDevice?
      var frontCamera: AVCaptureDevice?
      var currentCamera: AVCaptureDevice?
      var photoOutPut: AVCapturePhotoOutput?
      var cameraPreviewLayer: AVCaptureVideoPreviewLayer?
      var image: UIImage?
      var usingFrontCamera = false   
      override func awakeFromNib() {
          super.awakeFromNib()
          setupCaptureSession()
          setupDevice()
          setupInput()
          setupPreviewLayer()
          startRunningCaptureSession()
      }
      func setupCaptureSession(){
          captureSession.sessionPreset = AVCaptureSession.Preset.photo
      }
      func setupDevice(usingFrontCamera:Bool = false){
          let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)
          let devices = deviceDiscoverySession.devices   
          for device in devices {
              if device.position == AVCaptureDevice.Position.back {
                  backCamera = device
              } else if device.position == AVCaptureDevice.Position.front {
                  frontCamera = device
              }
          }
          if usingFrontCamera {
              currentCamera = frontCamera
          } else {
              currentCamera = backCamera
          }
      }
      func setupInput() {
          do {
              let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!)
              captureSession.addInput(captureDeviceInput)
              photoOutPut = AVCapturePhotoOutput()
              photoOutPut?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format:[AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: nil)
              captureSession.addOutput(photoOutPut!)
          } catch {
              print(error)
          }
      }
      func setupPreviewLayer(){
          cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
          cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
          cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
          cameraPreviewLayer?.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
          self.layer.insertSublayer(cameraPreviewLayer!, at: 0)
      }
      func startRunningCaptureSession(){
          captureSession.startRunning()
      }
      @IBAction func cameraButton_TouchUpInside(_ sender: Any) {
          let settings = AVCapturePhotoSettings()
          photoOutPut?.capturePhoto(with: settings, delegate: self as! AVCapturePhotoCaptureDelegate)
      }
      //Flip to front and back camera
      @IBAction func FlipThe_camera(_ sender: UIButton) {
          print("Flip touched")
          if let inputs = captureSession.inputs as? [AVCaptureDeviceInput] {
              for input in inputs {
                  captureSession.removeInput(input)
              }
          }
          usingFrontCamera = !usingFrontCamera
          setupCaptureSession()
          setupDevice(usingFrontCamera:usingFrontCamera)
          setupInput()
          setupPreviewLayer()
          startRunningCaptureSession()
      }
  }
  </code></pre>
  
  <blockquote>
    <p>collectionViewAttemptOfPagingAttempt002[2908:834004] <strong>* Terminating
    app due to uncaught exception 'NSInvalidArgumentException', reason:
    '*</strong> -[AVCaptureSession addOutput:] Cannot add output
     to capture session
    
       ->
        ->  because more than one output of the same type is
    unsupported'</p>
  </blockquote>