2011-04-10 91 views
1

當使用AVCaptureSessionPresetMediumAVCaptureSessionPresetMedium圖像大小?

// Create the session 
AVCaptureSession * newSession = [[AVCaptureSession alloc] init]; 

// Configure our capturesession 
newSession.sessionPreset = AVCaptureSessionPresetMedium; 

有什麼辦法來動態地告訴這將解決爲寬度x高度?很顯然,我可以等到一個委託像

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

被調用,並確定它在那裏,但我寧願做提前,這樣我可以預先計算一些數值的性能的原因。

回答

4

我會很樂意被證明是錯誤的這一點,但下面的步驟似乎是正確的方式,如果你不想硬編碼的數字:

-(CGSize)cameraSizeForCameraInput:(AVCaptureDeviceInput*)input 
{ 
     NSArray *ports = [input ports]; 
     AVCaptureInputPort *usePort = nil; 
     for (AVCaptureInputPort *port in ports) 
     { 
       if (usePort == nil || [port.mediaType isEqualToString:AVMediaTypeVideo]) 
       { 
         usePort = port; 
       } 
     } 

     if (usePort == nil) return CGSizeZero; 

     CMFormatDescriptionRef format = [usePort formatDescription]; 
     CMVideoDimensions dim = CMVideoFormatDescriptionGetDimensions(format); 

     CGSize cameraSize = CGSizeMake(dim.width, dim.height); 

     return cameraSize; 
} 

這有被稱爲調用startRunning後,否則結果爲0,0。我不知道未來會發生什麼,所以這就是爲什麼我要循環使用ports陣列。

+0

這工作完美,謝謝! – Kyle 2011-04-10 17:43:02