ios - CMFormatDescriptionRef 和 CMVideoFormatDescriptionGetDimensions

标签 ios ios8 avfoundation

更新到 ios 8 后,我似乎无法获得 CMFormatDescriptionRefs Dimensions 它总是返回 0,0 维。已经看过 apples 文档,但我真的不明白如何应用它。

这是我在 ios 7 上使用的现有代码(已经在我的 ios 7 设备上测试过了)

   NSArray *ports = [input ports];
AVCaptureInputPort *usePort = nil;
for ( AVCaptureInputPort *port in ports )
{
    if ( usePort == nil || [port.mediaType isEqualToString:AVMediaTypeVideo] )
    {

        usePort = port;
        break;
    }
}

CMVideoFormatDescriptionRef format = [usePort formatDescription];
CMVideoDimensions dim = CMVideoFormatDescriptionGetDimensions(format);
cameraSize = CGSizeMake(dim.width, dim.height);

最佳答案

通过为 AVcapture 端口添加观察者解决了我的问题 formatDescription 因为在 ios 8 版本中,使用获取端口格式描述的旧方法为您提供了一个“0”值。

第一行为AVCapture添加了一个观察者

[[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(avCaptureInputPortFormatDescriptionDidChangeNotification:)
                                                     name:AVCaptureInputPortFormatDescriptionDidChangeNotification object:nil];`

如果 AVCapture 发生变化,下一行将为您提供端口描述

- (void)avCaptureInputPortFormatDescriptionDidChangeNotification:(NSNotification *)notification {
[self initializeAPI];
NSArray *ports = [deviceInput ports];
AVCaptureInputPort *usePort = nil;
for ( AVCaptureInputPort *port in ports )
{
    if ( usePort == nil || [port.mediaType isEqualToString:AVMediaTypeVideo] )
    {

        usePort = port;
        break;
    }
}

CMFormatDescriptionRef formatDescription = usePort.formatDescription;
if (formatDescription) {
    CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
    if ((dimensions.width == 0) || (dimensions.height == 0)) {


        return;
    }
    CGFloat aspect = (CGFloat)dimensions.width / (CGFloat)dimensions.height;
    BOOL is7x = floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1;
    if (is7x) {
    }else{
        aspect = 1.f / aspect;

        dimensionForIOS8Above = CGSizeMake(dimensions.width, dimensions.height);
        //NSLog(@"8DIMENSION %f",dimensionForIOS8Above.width);



    }



}

关于ios - CMFormatDescriptionRef 和 CMVideoFormatDescriptionGetDimensions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26175409/

相关文章:

ios - 将参数传递给 dispatch_async

ios - AVFoundation 以 MP4 格式录制视频

ios - AVCaptureDevice 上的平滑焦点渐变

ios - 使用 AVFoundation 切换相机时显示红色状态栏

Android 和 iPhone UI 组件比较

iphone - 如何检测iPhone是否震动?

swift - 使用 StoryBoards 设置 UIToolBar 垂直设置

iOS 8 和 App 预览

ios - selectedViewcontroller 不适用于 tvOS 10 中的 tabbarcontroller

ios - SwiftUI 中的 Gridview