Knowing resolution of AVCaptureSession's session presets

Francisco Ryan Tolmasky I picture Francisco Ryan Tolmasky I · Oct 17, 2011 · Viewed 18k times · Source

I'm accessing the camera in iOS and using session presets as so:

captureSession.sessionPreset = AVCaptureSessionPresetMedium;

Pretty standard stuff. However, I'd like to know ahead of time the resolution of the video I'll be getting due to this preset (especially because depending on the device it'll be different). I know there are tables online you can look this up (such as here: http://cmgresearch.blogspot.com/2010/10/augmented-reality-on-iphone-with-ios40.html ). But I'd like to be able to get this programmatically so that I'm not just relying on magic numbers.

So, something like this (theoretically):

[captureSession resolutionForPreset:AVCaptureSessionPresetMedium];

which might return a CGSize of { width: 360, height: 480}. I have not been able to find any such API, so far I've had to resort to waiting to get my first captured image and querying it then (which for other reasons in my program flow is not good).

Answer

Christian Beer picture Christian Beer · Oct 17, 2011

I am no AVFoundation pro, but I think the way to go is:

captureSession.sessionPreset = AVCaptureSessionPresetMedium;
AVCaptureInput *input = [captureSession.inputs objectAtIndex:0]; // maybe search the input in array
AVCaptureInputPort *port = [input.ports objectAtIndex:0];
CMFormatDescriptionRef formatDescription = port.formatDescription;
CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);

I'm not sure about the last step and I didn't try it myself. Just found that in the documentation and think it should work.

Searching for CMVideoDimensions in Xcode you'll find the RosyWriter example project. Have a look at that code (I don't have time to do that now).