title: Capturing iSight image using AVFoundation on Mac
body: <p>I previously had this code to capture a single image from a Mac's iSight camera using QTKit:</p>
  
  <pre><code>- (NSError*)takePicture
  {    
      BOOL success;
      NSError* error;
  
      captureSession = [QTCaptureSession new];
      QTCaptureDevice* device = [QTCaptureDevice defaultInputDeviceWithMediaType: QTMediaTypeVideo];
  
      success = [device open: &amp;error];
      if (!success) { return error; }
  
      QTCaptureDeviceInput* captureDeviceInput = [[QTCaptureDeviceInput alloc] initWithDevice: device];
      success = [captureSession addInput: captureDeviceInput error: &amp;error];
      if (!success) { return error; }
  
      QTCaptureDecompressedVideoOutput* captureVideoOutput = [QTCaptureDecompressedVideoOutput new];
      [captureVideoOutput setDelegate: self];
  
      success = [captureSession addOutput: captureVideoOutput error: &amp;error];
      if (!success) { return error; }
  
      [captureSession startRunning];
      return nil;
  }
  - (void)captureOutput: (QTCaptureOutput*)captureOutput
    didOutputVideoFrame: (CVImageBufferRef)imageBuffer
       withSampleBuffer: (QTSampleBuffer*)sampleBuffer
         fromConnection: (QTCaptureConnection*)connection
  {
      CVBufferRetain(imageBuffer);
  
      if (imageBuffer) {
          [captureSession removeOutput: captureOutput];
          [captureSession stopRunning];
  
          NSCIImageRep* imageRep = [NSCIImageRep imageRepWithCIImage: [CIImage imageWithCVImageBuffer: imageBuffer]];
  
          _result = [[NSImage alloc] initWithSize: [imageRep size]];
          [_result addRepresentation: imageRep];
  
          CVBufferRelease(imageBuffer);
  
          _done = YES;
      }
  }
  </code></pre>
  
  <p>However, I found today that QTKit has been deprecated and so we must now use AVFoundation.
  Can anyone help me convert this code to its AVFoundation equivalent? It seems as though many methods have the same name, but at the same time, a lot is different and I'm at a complete loss here... Any help?</p>