title: Swift : take photo from AVFoundation
body: <p>I want to translate this func from obj c to swift, buti can't translate a part of the code.
  Could someone explain me how to take photo from AVFondation or help me to translate this function ?</p>
  
  <pre><code>- (void) capImage { //method to capture image from AVCaptureSession      video feed
       AVCaptureConnection *videoConnection = nil;
       for (AVCaptureConnection *connection in stillImageOutput.connections) {
  
    for (AVCaptureInputPort *port in [connection inputPorts]) {
  
    if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
      videoConnection = connection;
          break;
    }
    }
  
     if (videoConnection) {
      break;
   }
  }
  
   NSLog(@"about to request a capture from: %@", stillImageOutput);
  [stillImageOutput     captureStillImageAsynchronouslyFromConnection:videoConnection          completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
  
    if (imageSampleBuffer != NULL) {
     NSData *imageData = [AVCaptureStillImageOutput        jpegStillImageNSDataRepresentation:imageSampleBuffer];
  [self processImage:[UIImage imageWithData:imageData]];
   }
  }]; }
  </code></pre>
  
  <p>What i did but not working :</p>
  
  <pre><code>  func takePhoto(sender:UIButton){
  
    var videoConnection:AVCaptureConnection
    var connection:AVCaptureConnection
     var port : AVCaptureInputPort
  
   for connection in stillImageOutput?.connections {
  
  for (port in connection.inputPorts as AVCaptureInputPort) {
  
  
      if port = AVMediaTypeVideo {
          videoConnection = connection
          break
      }
  
  }
  
      if videoConnection {
          break
     }
  
  
   }
  stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection,      completionHandler: {(imageSampleBuffer, error) in
  if (imageSampleBuffer != nil) {
      var imageData =      AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer as CMSampleBuffer)
      var image: UIImage = UIImage(data: imageData)
  
  
  }
  })
  
  
  }
  </code></pre>
  
  <p>Could someone help me ?</p>