两者的区别是什么
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错误。为什么?
发布于 2017-07-13 09:55:11
在您的第一个代码示例中,存储在destinationImageBufferAttrs中的引用是拥有的,必须稍后使用CFRelease释放(或传输到ARC控件)。
在第二个代码示例中,存储在destinationImageBufferAttrs中的引用处于ARC控制之下,并且ARC可以在赋值后立即释放它,因为不再有ARC拥有的引用。
将__bridge更改为__bridge_retained将所有权从ARC转移到您自己的代码,然后您将负责调用该对象的CFRelease。
发布于 2017-07-13 09:46:57
结果是,当我想再次访问时,在放入CFDictionaryRef之后,@{}文本没有保留。所以下面的代码将会起作用:
NSDictionary *dic = @{(NSString*)kCVPixelBufferPixelFormatTypeKey:@(kCVPixelFormatType_32BGRA)}; // "dic" reference will retain the nsdic created with @ literal
CFDictionaryRef destinationImageBufferAttrs = (__bridge CFDictionaryRef)dic;https://stackoverflow.com/questions/45070041
复制相似问题