2011-04-10 41 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