iOS8视频维度,CMVideoDimensions返回0,0

时间:2014-09-17 23:50:51

标签: cocoa-touch ios8

iOS8中的

返回的维度为0,0

CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);

这适用于iOS7,所以如何知道支持的视频维度,因为我需要知道视频宽高比

3 个答案:

答案 0 :(得分:5)

您需要等待 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;
        }

    }

}

答案 1 :(得分:0)

答案 2 :(得分:0)

我最近遇到了这个特殊问题,以下是也需要它的用户的Swift 5版本:

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