向NSDictionary添加结构 [英] Adding a structure to a NSDictionary

查看:383
本文介绍了向NSDictionary添加结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码凭空创建一个CMVideoFormatDescriptionRef:

I am creating a CMVideoFormatDescriptionRef out of thin air using this code:

  CMVideoDimensions dimensions = {
    .width = width,
    .height = height
  };

  CMVideoFormatDescriptionRef videoInfo = NULL;

  NSDictionary *options2 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @(YES), kCVPixelBufferCGImageCompatibilityKey,
                           @(YES), kCVPixelBufferCGBitmapContextCompatibilityKey,
                           dimensions, kCVImageBufferDisplayDimensionsKey,
                            nil];
  CFDictionaryRef dictOptionsRef = (__bridge CFDictionaryRef)options2;

  CMFormatDescriptionCreate(kCFAllocatorDefault, kCMMediaType_Video, 'brga', dictOptionsRef, &videoInfo);

我需要将该字典传递给CMFormatDescriptionCreate.

I need to pass that dictionary to CMFormatDescriptionCreate.

问题是最后一行

dimensions, kCVImageBufferDisplayDimensionsKey,

这不是将dimensions添加到该字典的正确方法.它使应用程序崩溃.

This is not the correct way to add dimensions to that dict. It crashes the app.

我尝试使用以下方法将整个内容包装在NSValue上:

I have tried to wrap the whole thing on a NSValue using this:

NSValue *miValue = [NSValue value: &dimensions
                     withObjCType:@encode(CMVideoDimensions)];

理论上它可以正常工作,没有崩溃,但是videoInfo创建不正确(OSStatus -12731).如果我从字典中删除最后一行,则会创建videoInfo,但没有定义尺寸的定义,这些尺寸假定为0x0像素或codecType.见下文:

It works in theory, no crash, but videoInfo is not created correctly (OSStatus -12731). If I remove this last line from the dictionary, videoInfo is created but has no definition for the dimensions, that are assumed to be 0x0 pixels, or codecType. See below:

<CMVideoFormatDescription 0x17044c270 [0x1b0330bb8]> {
    mediaType:'vide' 
    mediaSubType:'brga' 
    mediaSpecific: {
        codecType: 0        dimensions: 0 x 0    <--HERE!!!
    } 
    extensions: {<CFBasicHash 0x17086dcc0 [0x1b0330bb8]>{type = immutable dict, count = 4,
entries =>
    0 : <CFString 0x1aa9427c8 [0x1b0330bb8]>{contents = "CVImageBufferYCbCrMatrix"} = <CFString 0x1aa942808 [0x1b0330bb8]>{contents = "ITU_R_601_4"}
    3 : <CFString 0x1aa942c68 [0x1b0330bb8]>{contents = "CGImageCompatibility"} = <CFBoolean 0x1b0331110 [0x1b0330bb8]>{value = true}
    5 : <CFString 0x1aa9428a8 [0x1b0330bb8]>{contents = "CVImageBufferColorPrimaries"} = <CFString 0x1aa9427e8 [0x1b0330bb8]>{contents = "ITU_R_709_2"}
    6 : <CFString 0x1aa942c48 [0x1b0330bb8]>{contents = "CGBitmapContextCompatibility"} = <CFBoolean 0x1b0331110 [0x1b0330bb8]>{value = true}
}
}
}

常规CMSampleBufferRef中的CMVideoFormatDescriptionRefcodecType显示为BGRA,并显示框架的常规尺寸,例如1920x1080.

A CMVideoFormatDescriptionRef from a regular CMSampleBufferRef shows codecType as BGRA and the regular dimensions of the frame, 1920x1080, for example.

我该怎么做?

推荐答案

kCVImageBufferDisplayDimensionsKey头文件表示密钥的值为a

The kCVImageBufferDisplayDimensionsKey header file says the key's value is a

//CFDictionary,具有以下两个键

// CFDictionary with the following two keys

kCVImageBufferDisplayWidthKeykCVImageBufferDisplayHeightKey,它们都是CFNumber s.

您传递的是CMVideoDimensions,因此请将其更改为

You're passing CMVideoDimensions instead, so change that to

NSDictionary *dimensions = [NSDictionary dictionaryWithObjectsAndKeys:@(width), kCVImageBufferDisplayWidthKey, @(height), kCVImageBufferDisplayHeightKey];

这篇关于向NSDictionary添加结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆