开发者

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜