title: NSGenericException, reason: [AVCapturePhotoOutput capturePhotoWithSettings:delegate:] No active and enabled video connection
body: <p>I keep getting the above error message when trying to run my app and capture a photo.</p>
  
  <p>I am new to programming so if anyone can help it would be awesome.</p>
  
  <p>Here is my code</p>
  
  <pre><code>import UIKit
  import AVFoundation
  import Firebase
  
  
  class CameraViewController: UIViewController, AVCapturePhotoCaptureDelegate, UITextViewDelegate, UITextFieldDelegate {
  
  @IBOutlet weak var cameraView: UIView!
  @IBOutlet weak var cancelButton: UIButton!
  @IBOutlet weak var topButton: UIButton!
  @IBOutlet weak var switchCamera: UIButton!
  @IBOutlet weak var captionButton: UIButton!
  @IBOutlet weak var takePhoto: UIButton!
  @IBOutlet weak var postButton: UIButton!
  
  
  
  var captureSession = AVCaptureSession()
  var sessionOutput = AVCapturePhotoOutput()
  var previewLayer = AVCaptureVideoPreviewLayer()
  var settings = AVCapturePhotoSettings()
  
  
  var ref = FIRDatabase.database().reference()
  let storage = FIRStorage.storage()
  
  
  
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      // Do any additional setup after loading the view.
  }
  
  override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)
  
      let deviceSession = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInDualCamera, .builtInTelephotoCamera, .builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: .unspecified)
  
      for device in (deviceSession?.devices)!{
  
          if device.position == AVCaptureDevicePosition.front {
  
              do{
                  let input = try AVCaptureDeviceInput(device: device)
                  if captureSession.canAddInput(input) {
                      captureSession.addInput(input)
  
                      if captureSession.canAddOutput(sessionOutput) {
                          captureSession.canAddOutput(sessionOutput)
  
                          previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                          previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
                          previewLayer.connection.videoOrientation = .portrait
  
                          cameraView.layer.addSublayer(previewLayer)
  
  
                          previewLayer.position = CGPoint (x: self.cameraView.frame.width / 2, y: self.cameraView.frame.height / 2)
                          previewLayer.bounds = cameraView.frame
  
                          captureSession.startRunning()
                      }
                  }
  
              }catch let avError {
                  print (avError)
              }
          }
      }
  }
  
  
  
  func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
  
      if let error = error {
          print(error.localizedDescription)
          return
      }
  
      AppDelegate.instance().showActivityIndicator()
  
      let uid = FIRAuth.auth()!.currentUser!.uid
      let ref = FIRDatabase.database().reference()
      let storage = FIRStorage.storage().reference(forURL: "my url")
  
      if let sampleBuffer = photoSampleBuffer, let previewBuffer = photoSampleBuffer, let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {
  
          let key = ref.child("posts").childByAutoId().key
          let imageRef = storage.child("posts").child(uid).child("\(key).jpg")
  
          let uploadTask = imageRef.put(dataImage, metadata: nil) {(metadata, error) in
              if error != nil {
                  print(error!.localizedDescription)
                  AppDelegate.instance().dismissActivityIndicator()
                  return
              }
              imageRef.downloadURL(completion: {(url, error) in
                  if let url = url {
                      let feed = ["userId" : uid,
                                  "pathToImage" : url.absoluteString,
                                  "likes" : 0,
                                  "author" : FIRAuth.auth()?.currentUser!.displayName! as Any,
                                  "postId" : key] as [String : Any]
  
                      let postFeed = ["\(key)" : feed]
  
                      ref.child("posts").updateChildValues(postFeed)
                      AppDelegate.instance().dismissActivityIndicator()
  
                      self.dismiss(animated: true, completion: nil)
                      self.postButton.isHidden = false
                  }
              })
  
  
          }
          uploadTask.resume()
      }
      captureSession.stopRunning()
      previewLayer.removeFromSuperlayer()
  
  }
  @IBAction func takePhoto(_ sender: Any) {
      let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!
      let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String : previewPixelType, kCVPixelBufferWidthKey as String : 160, kCVPixelBufferHeightKey as String : 160]
  
      settings.previewPhotoFormat = previewFormat
      self.sessionOutput.capturePhoto(with: settings, delegate: self)
      sessionOutput.isLivePhotoCaptureEnabled = true
  
  
  
  }
  
  @IBAction func cancelPressed(_ sender: Any) {
  
      dismiss(animated: true, completion: nil)
  
  }
  
  override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
      self.view.endEditing(true)
  }
  func textFieldShouldReturn(_ textField: UITextField) -&gt; Bool {
      textField.resignFirstResponder()
      return false
  
  }
  
  
  }
  </code></pre>
  
  <p>I imagine the error is happening somewhere at this line
   <code>self.sessionOutput.capturePhoto(with: settings, delegate: self)</code></p>
  
  <p>I am not sure however. I would really really appreciate some help. Thanks!</p>