开发者

How to convert CFDataRef into NSData?

开发者 https://www.devze.com 2023-01-17 12:54 出处:网络
I have CFDataRef and I want to convert it into NSData. CGDataProviderRef dataProvider = CGImageGetDataProvider(screenShot);

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

精彩评论

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