2014-10-05 47 views
-2

我發現了代碼,我想在一個快速的ios應用程序中使用,但我很難快速寫入它。如何在swift中編寫這個void objective-c函數

-(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:)) { 

} 
+0

發表您的SWIFT代碼的企圖? – 2014-10-05 18:21:08

+0

添加了我自己的嘗試 – iamjonesy 2014-10-05 18:26:30

+0

這不是一個有效的問題嗎?爲什麼倒票加上關閉? – iamjonesy 2014-10-05 18:33:18

回答

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

} 
+0

感謝搶劫幫助! – iamjonesy 2014-10-05 21:16:52