3

in iOS8 the dimension returned is 0,0

CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);

This was working on iOS7, so how to know the supported video dimension, as i need to know the video aspect ratio

Peter Lapisu
  • 18,394
  • 14
  • 107
  • 163

3 Answers3

5

You need to wait for the AVCaptureInputPortFormatDescriptionDidChangeNotification

- (void)avCaptureInputPortFormatDescriptionDidChangeNotification:(NSNotification *)notification {

    AVCaptureInput *input = [self.recorder.captureSession.inputs objectAtIndex:0];
    AVCaptureInputPort *port = [input.ports objectAtIndex:0];
    CMFormatDescriptionRef formatDescription = port.formatDescription;
    if (formatDescription) {
        CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
        if ((dimensions.width == 0) || (dimensions.height == 0)) {
            return;
        }
        CGFloat aspect = (CGFloat)dimensions.width / (CGFloat)dimensions.height;

        if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1) {
            // since iOS8 the aspect ratio is inverted
            // remove this check if iOS7 will not be supported
            aspect = 1.f / aspect;
        }

    }

}
Peter Lapisu
  • 18,394
  • 14
  • 107
  • 163
  • 2
    Couldn't you use just check the `activeFormat` property on the current device? https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVCaptureDevice_Class/index.html#//apple_ref/occ/instp/AVCaptureDevice/activeFormat – Crashalot Feb 18 '16 at 01:06
2

Provided you're tracking the device being used, you can access the current format from activeFormat: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVCaptureDevice_Class/index.html#//apple_ref/occ/instp/AVCaptureDevice/activeFormat

Crashalot
  • 31,452
  • 56
  • 235
  • 393
0

I recently ran into this particular issue, here's the Swift 5 version for those who need it too:

import Foundation
import AVFoundation

class MySessionManager: NSObject {
    static let notificationName = "AVCaptureInputPortFormatDescriptionDidChangeNotification"
    let session: AVCaptureSession

    var videoCaptureDimensions: CMVideoDimensions?

    init(session: AVCaptureSession) {
       self.session = session

       let notificationName = NSNotification.Name()

       NotificationCenter.default.addObserver(
           self, 
           selector: #selector(formatDescription(didChange:)),
           name: .init(Self.notificationName), 
           object: nil
       )
    }

    deinit { NotificationCenter.default.removeObserver(self) }

    @objc func formatDescription(didChange notification: NSNotification) {
        guard
            let input = session.inputs.first,
            let port = input.ports.first,
            let formatDesc = port.formatDescription
        else { return }

        var dimensions = CMVideoFormatDescriptionGetDimensions(formatDesc)

        // ... perform any necessary dim adjustments ...

        videoCaptureDimensions = dimensions
    }
}
Tristian
  • 3,001
  • 5
  • 24
  • 44