如何在swift中编写这个void objective-c函数

时间:2014-10-05 18:19:17

标签: ios objective-c function swift

我已经找到了我想在swift ios应用程序中使用的代码,但我很难快速编写代码。

-(void) captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection
{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer( sampleBuffer );
    CGSize imageSize = CVImageBufferGetEncodedSize( imageBuffer );
    // also in the 'mediaSpecific' dict of the sampleBuffer

   NSLog( @"frame captured at %.fx%.f", imageSize.width, imageSize.height );
}

它只是我遇到问题的功能声明,功能的内容应该更容易。我不习惯语法:

    -(void) captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection

我的尝试:

我开始尝试这样写,但当()内没有任何内容会自动完成时,我意识到这肯定是错的:

func captureOutput(AVCaptureOutput(didOutputSampleBuffer:)) {

}

1 个答案:

答案 0 :(得分:4)

您可以在AVCaptureVideoDataOutputSampleBufferDelegate Protocol Reference中找到正确的声明,声明如下:

optional func captureOutput(_ captureOutput: AVCaptureOutput!,
    didOutputSampleBuffer sampleBuffer: CMSampleBuffer!,
    fromConnection connection: AVCaptureConnection!)

但是,您不会在方法定义中写optional

class MyDelegate: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {

    func captureOutput(captureOutput:AVCaptureOutput,
        didOutputSampleBuffer sampleBuffer:CMSampleBuffer,
        fromConnection connection:AVCaptureConnection) {

            let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
            let imageSize = CVImageBufferGetEncodedSize(imageBuffer)

            NSLog("frame captured at %.fx%.f", imageSize.width, imageSize.height)
    }

}