How to convert CFDataRef into NSData?
I have CFDataRef
and I want to convert it into NSData
.
CGDataProviderRef dataProvider = CGImageGetDataProvider(screenShot);
CFDataRef data = CGDataProviderCopyData(dataProvider);
CGDataProviderRelease(dataProvider);
NSString *imagePathLocation = [destinationPath stringByAppendingString:[@"/" stringByAppendingString:DesktopImage]];
[[NSFileManager defaultManager]createFileAtPath: imagePathLocation contents:nil a开发者_运维知识库ttributes:nil];
fileHandleForImage = [NSFileHandle fileHandleForWritingAtPath:imagePathLocation];
NSData * data1 = (NSData *)data;
[fileHandleForImage writeData:data1];
When I typecast (NSData * data1 = (NSData *)data
) it image gets corrupted.
Can anyoone help?
It's toll-free bridged, so you just have to cast it.
NSData *myData = (__bridge NSData *)myCFDataRef;
As @KrisMarkel said, you just have to transfer ownership to ARC like so:
Like so to transfer ownership:
NSData* data1 = (__bridge_transfer NSData*) data;
else to use the reference without transferring ownership:
NSData* data2 = (__bridge NSData*) data;
精彩评论