What's the difference between
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);
and
CFDictionaryRef destinationImageBufferAttrs = (__bridge CFDictionaryRef)(@{(NSString*)kCVPixelBufferPixelFormatTypeKey:@(kCVPixelFormatType_32BGRA)});
?
I am getting an EXC_BAD_ACCESS error if I use the second one. Why?
In your first code sample, the reference stored into
destinationImageBufferAttrsis owned and must be later released usingCFRelease(or transferred to ARC control).In the second code sample, the reference stored into
destinationImageBufferAttrsis under ARC control and ARC can free it immediately after the assignment as there are no longer ARC-owned references to it.Change
__bridgeto__bridge_retainedto transfer ownership from ARC to your own code, you will then be responsible for callingCFReleasefor the object.