title: Inactive/invalid connection passed when attempting to take a photo
body: <p>I am attempting to take a photo using a custom view (without using <a href="https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIImagePickerController_Class/index.html" rel="nofollow"><code>UIImagePickerController</code></a>), but whenever I attempt to take the photo, the app crashes, and this error is thrown:</p>
  
  <blockquote>
    <p>Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection:completionHandler:] - inactive/invalid connection passed.'</p>
  </blockquote>
  
  <p>Here's my <code>takePhoto()</code> function, which causes the error:</p>
  
  <pre><code>func takePhoto(sender: UIButton!){
      var still: AVCaptureStillImageOutput = AVCaptureStillImageOutput()
      var connection: AVCaptureConnection = AVCaptureConnection(inputPorts: self.input.ports, output: still)
  
      if(connection.enabled){
          still.captureStillImageAsynchronouslyFromConnection(connection, completionHandler: {(buffer: CMSampleBuffer!, error: NSError!) -&gt; Void in
              println("picture taken")//this never gets executed
          })
      }
  }
  </code></pre>
  
  <p>I've also tried setting the <code>connection</code> variable in the <code>takePhoto()</code> function to:</p>
  
  <pre><code>self.output.connections[0] as AVCaptureConnection
  </code></pre>
  
  <p>as well as</p>
  
  <pre><code>(self.session.outputs[0] as AVCaptureOutput).connections[0] as AVCaptureConnection
  </code></pre>
  
  <p>and I get the same result.</p>
  
  <p>On the same view as the <strong>take photo</strong> button, there is also a live preview of the camera (which works):</p>
  
  <pre><code>func setupCamera(){
      self.session = AVCaptureSession()
      self.session.sessionPreset = AVCaptureSessionPreset640x480
      self.session.beginConfiguration()
      self.session.commitConfiguration()
  
      var error: NSError?
  
      var devices: [AVCaptureDevice] = AVCaptureDevice.devices() as [AVCaptureDevice]
      for device in devices{
          if(device.hasMediaType(AVMediaTypeVideo) &amp;&amp; device.supportsAVCaptureSessionPreset(AVCaptureSessionPreset640x480)){
  
              //the input variable is initialized here
  
              self.input = AVCaptureDeviceInput.deviceInputWithDevice(device as AVCaptureDevice, error: &amp;error) as AVCaptureDeviceInput
  
              if(self.session.canAddInput(self.input)){
                  self.session.addInput(self.input)
                  break
              }
          }
      }
  
      var settings = [kCVPixelBufferPixelFormatTypeKey:kCVPixelFormatType_32BGRA]
  
      //the output variable is initialized here
  
      self.output = AVCaptureVideoDataOutput()
      self.output.videoSettings = settings
      self.output.alwaysDiscardsLateVideoFrames = true
  
      if(self.session.canAddOutput(self.output)){
          self.session.addOutput(self.output)
      }
  
      var captureLayer = AVCaptureVideoPreviewLayer(session: self.session)
      captureLayer.frame = CGRectMake(0, 64, self.view.frame.width, (self.view.frame.width * 4) / 3)
      self.view.layer.addSublayer(captureLayer)
  
      self.session.startRunning()
  }
  </code></pre>
  
  <p>I'm testing this on my iPhone 5s, and everything works, including the preview, except for the <code>takePhoto()</code> function.</p>
  
  <p>Is there any way to do this, or do I have to use <a href="https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIImagePickerController_Class/index.html" rel="nofollow"><code>UIImagePickerController</code></a>?</p>