Search code examples
iphonevideo-processingavcapturesession

AVCaptureSessionPresetMedium image size?


When using AVCaptureSessionPresetMedium

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

// Configure our capturesession
newSession.sessionPreset = AVCaptureSessionPresetMedium;

Is there any way to dynamically tell what this will resolve to for width x height? Obviously I can wait until a delegate like

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

Gets called and determine it there, but I would rather do it in advance so that I can precalculate some values for performance reasons.


Solution

  • I'd be very happy to be proven wrong on this, but the steps below seem to be the proper way if you don't want to hardcode the numbers:

    -(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;
    }
    

    This has to be called after the startRunning call, otherwise the result is 0,0. I don't know what to expect in the future, so that's why I loop over the ports array.