0

I'm converting an Objective C code into Swift to take camera pictures. But I'm having a hard time converting the blocks parameter into Swift closure of the captureStillImageAsynchronouslyFromConnection function. I've followed the blocks to closure steps in this tutorial swift-blocks-tutorial but still no luck.

Here's my Objective C code

[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

   if (imageDataSampleBuffer)
   {
        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
        UIImage *image = [[UIImage alloc] initWithData:imageData];
        [[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:nil];
    }
}];

And Swift Code

var videoConnection: AVCaptureConnection = self.stillImageOutput.connectionWithMediaType(AVMediaTypeVideo)            
self.stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection,
            completionHandler: { (imageSampleBuffer: CMSampleBuffer!, error: NSError!) in

    println("Capture still image!")
    if imageSampleBuffer {
          //TODO: Save image here
    }

 })

I'm getting this error in my Swift code

Use of module 'CMSampleBuffer' as a type
'CMSampleBuffer!' is not a subtype of '<<error type>>'
neztreh
  • 3,971
  • 3
  • 19
  • 17

0 Answers0