如何将Objective C块转换为Swift Closure?

时间:2014-07-18 19:46:32

标签: objective-c swift closures block avfoundation

我正在将一个Objective C代码转换为Swift来拍摄相机照片。但是我很难将blocks参数转换为captureStillImageAsynchronouslyFromConnection函数的Swift闭包。我已按照本教程swift-blocks-tutorial中的闭包步骤进行了操作,但仍然没有运气。

这是我的Objective C代码

[[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];
    }
}];

和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
    }

 })

我在Swift代码中收到此错误

Use of module 'CMSampleBuffer' as a type
'CMSampleBuffer!' is not a subtype of '<<error type>>'

0 个答案:

没有答案