0

有什么区别

const void *keys[] = { kCVPixelBufferPixelFormatTypeKey };
OSStatus pixelFormatType = kCVPixelFormatType_32BGRA;
CFNumberRef pixelFormatTypeRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pixelFormatType);
const void *values[] = { pixelFormatTypeRef };
CFDictionaryRef destinationImageBufferAttrs = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 1, NULL, NULL);

CFDictionaryRef destinationImageBufferAttrs = (__bridge CFDictionaryRef)(@{(NSString*)kCVPixelBufferPixelFormatTypeKey:@(kCVPixelFormatType_32BGRA)});

?

EXC_BAD_ACCESS如果我使用第二个,我会收到错误消息。为什么?

4

2 回答 2

1

在您的第一个代码示例中,存储到其中的引用destinationImageBufferAttrs是拥有的,并且必须稍后使用CFRelease(或转移到 ARC 控制)释放。

在第二个代码示例中,存储到的引用destinationImageBufferAttrs受 ARC 控制,并且 ARC 可以在分配后立即释放它,因为不再有 ARC 拥有的对它的引用。

更改__bridge__bridge_retained将所有权从 ARC 转移到您自己的代码,然后您将负责调用CFRelease该对象。

于 2017-07-13T01:55:10.507 回答
0

事实证明,当我想再次访问时,@{}文字并没有被保留。CFDictionaryRef所以下面的代码将改为:

NSDictionary *dic = @{(NSString*)kCVPixelBufferPixelFormatTypeKey:@(kCVPixelFormatType_32BGRA)}; // "dic" reference will retain the nsdic created with @ literal
CFDictionaryRef destinationImageBufferAttrs = (__bridge CFDictionaryRef)dic;
于 2017-07-13T01:46:56.580 回答