iosobjective-cavfoundation

Cannot read CMVideoDimensions structure with Objective C code


I have an AVCaptureDevice object at hand and would like to print the maximum supported photo dimensions as provided by activeFormat.supportedMaxPhotoDimensions (*), using Objective C. I tried the following:

for (NSValue *obj in device.activeFormat.supportedMaxPhotoDimensions) {
  CMVideoDimensions *vd = (__bridge CMVideoDimensions *)obj;
  NSString *s = [NSString stringWithFormat:@"res=%d:%d", vd->width, vd->height];
  //print that string
}

If I run this code, I get:

res=314830880:24994

This is way too high, and there is obviously something I am doing wrong, but I don't know what it could be? According to the information I see on the web, I should get something closer to 4000:3000.

I can successfully read device.activeFormat.videoFieldOfView and other fields, so I believe my code is sound overall.

(*) https://developer.apple.com/documentation/avfoundation/avcapturedevice/format/3967581-supportedmaxphotodimensions


Solution

  • The AVFoundation framework provides a CMVideoDimensionsValue property for NSValue as of iOS 16.

    Simply change your line:

    CMVideoDimensions *vd = (__bridge CMVideoDimensions *)obj;
    

    to:

    CMVideoDimensions *vd = obj.CMVideoDimensionsValue;
    

    Ensure this .m file imports AVFoundation. If not, you will get a compiler error about an unknown property.