Peter Lapisu
Peter Lapisu

Reputation: 21005

iOS8 video dimension, CMVideoDimensions returns 0,0

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

Upvotes: 4

Views: 2519

Answers (3)

Tristian
Tristian

Reputation: 3512

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

Upvotes: 0

Crashalot
Crashalot

Reputation: 34523

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

Upvotes: 2

Peter Lapisu
Peter Lapisu

Reputation: 21005

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

    }

}

Upvotes: 5

Related Questions