如何将cfdatarefcgimageref 转 nsdataa转换为NSData

转换一个NSObject的成NSData的-iphone-CodeGo.net
转换一个NSObject的成NSData的
我在转换有一个问题一个NSObject成NSData。我有一个类继承NSObject。
当我试图在特定类的对象转换为NSData如下:
NSData *dataOnObject = [NSKeyedArchiver archivedDataWithRootObject:classObject];
但是它给出了异常,表明-[classObject encodeWithCoder:]:无法识别的选择发送到实例..
我也加入对象到一个新创建的数组
NSMutableArray *wrapperedData = [NSMutableArray arrayWithObject: classObject];
NSData *dataOnObject = [NSKeyedArchiver archivedDataWithRootObject:value];
但尽管如此,它render了异常。
所以我需要从对象提取字节classObject。
任何帮助将不胜感激...
等待您的回复...
本文地址 :CodeGo.net/95806/
-------------------------------------------------------------------------------------------------------------------------
NSData *dataOnObject = [NSKeyedArchiver archivedDataWithRootObject:classObject];
NSData *dataOnObject = [[NSUserDefaults standardUserDefaults] objectForKey:@"someKey"];
但是,这只是在对已经保存读取数据。如果你想保存一个对象作为NSData的,那么你有这样的:
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:classObject] forKey:@"someKey"];
但是这还不是全部。您classObject有NSCoding协议,并有encodeWithCoder:这样和的initWithCoder:因为它不是一个NS的对象,以便为它工作。
您需要encodeWithCoder:这样对你的自定义类,序列化其所有的NSCoder传递给它。如果它的属性包括任何更多的自定义类,他们需要encodeWithCoder:这样太。
你只能支持存储对象NSCoding协议
您可以将任何对象转换为NSData的与NSCoding协议。
你可以找到示例代码在这里做到这一点:
您必须为自己的对象,例如:
- (void)encodeWithCoder:(NSCoder *)aCoder
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInt:self.age forKey:@"age"];
[aCoder encodeObject:self.email forKey:@"email"];
[aCoder encodeObject:self.password forKey:@"password"];
BOOL success = [NSKeyedArchiver archiveRootObject:person toFile:archiveFilePath];
- (id)initWithCoder:(NSCoder *)aDecoder
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntForKey:@"age"];
self.email = [aDecoder decodeObjectForKey:@"email"];
self.password = [aDecoder decodeObjectForKey:@"password"];
Person *unarchivePerson = [NSKeyedUnarchiver unarchiveObjectWithFile:archiveFilePath];
本文标题 :转换一个NSObject的成NSData的
本文地址 :CodeGo.net/95806/
Copyright (C) 2014 CodeGo.netHow to create CGImageRef from NSData string data (NOT UIImage) [如何创建从NSData字符串数据cgimageref(不uiimage)] - 问题-字节技术
How to create CGImageRef from NSData string data (NOT UIImage)
如何创建从NSData字符串数据cgimageref(不uiimage)
问题 (Question)
How does one create a new CGImageRef without a UIImage?
I can't use image.CGImage
I am receiving a base64 encoded image as a std::string from a server process. The first part of the code below simulates receiving the encoded string.
- (UIImage *)testChangeImageToBase64String
UIImage *processedImage = [UIImage imageNamed:@"myFile.jpg"];
// UIImage to unsigned char *
CGImageRef imageRef = processedImage.CGI
NSData *data = (NSData *) CFBridgingRelease(CGDataProviderCopyData(CGImageGetDataProvider(imageRef)));
// encode data to Base64 NSString
NSString *base64EncodedDataString = [data base64EncodedStringWithOptions:0];
// create encoded std::string
std::string encoded([base64EncodedDataString UTF8String]);
// ***************************************************************************
// This is where we call the server method and receive the bytes in a std::string
std::string received =
// ***************************************************************************
// get Base64 encoded std::string into NSString
NSString *base64EncodedCstring = [NSString stringWithCString:encoded.c_str() encoding:[NSString defaultCStringEncoding]];
// NSData from the Base64 encoded std::string
NSData *nsdataFromBase64String = [[NSData alloc]initWithBase64EncodedString:base64EncodedCstring options:0];
Everything is good!!!!..... until I try to populate the newImage.
When I get the encoded string, I need to get a CGImageRef to get the data back into the correct format to populate a UIImage. If the data is not in the correct format the UIImage will be nil.
I need to create a new CGImageRef with the nsdataFromBase64String.
Something like:
CGImageRef base64ImageRef = [newCGImageRefFromString:nsdataFromBase64String];
Then I can use imageWithCGImage to put the data into a new UIImage.
Something like:
UIImage *imageFromImageRef = [UIImage imageWithCGImage: base64ImageRef];
Then I can return the UIImage.
return newI
Please note that the following line will NOT work:
UIImage *newImage = [[UIImage alloc] initWithData:nsdataFromBase64String];
The data needs to be in the correct format or the UIImage will be nil.
Hence, my question, "How do I create a CGImageRef with NSData?"
如何创建一个新的cgimageref没有uiimage?我不能使用image.CGImage我收到的Base64编码的图像作为一个std::从一个服务器进程的字符串。下面的代码的第一部分模拟接收编码的字符串。- (UIImage *)testChangeImageToBase64String
UIImage *processedImage = [UIImage imageNamed:@"myFile.jpg"];
// UIImage to unsigned char *
CGImageRef imageRef = processedImage.CGI
NSData *data = (NSData *) CFBridgingRelease(CGDataProviderCopyData(CGImageGetDataProvider(imageRef)));
// encode data to Base64 NSString
NSString *base64EncodedDataString = [data base64EncodedStringWithOptions:0];
// create encoded std::string
std::string encoded([base64EncodedDataString UTF8String]);
// ***************************************************************************
// This is where we call the server method and receive the bytes in a std::string
std::string received =
// ***************************************************************************
// get Base64 encoded std::string into NSString
NSString *base64EncodedCstring = [NSString stringWithCString:encoded.c_str() encoding:[NSString defaultCStringEncoding]];
// NSData from the Base64 encoded std::string
NSData *nsdataFromBase64String = [[NSData alloc]initWithBase64EncodedString:base64EncodedCstring options:0];
一切都很好!!!.....!直到我尝试在新的。当我得到的编码的字符串,我需要一个CGImageRef获得的数据返回到正确的格式来填充uiimage。如果数据格式不正确的uiimage将零。
我需要创建新的nsdatafrombase64string cgimageref。像:
CGImageRef base64ImageRef = [newCGImageRefFromString:nsdataFromBase64String];
然后我可以使用imagewithcgimage把数据转换成一个新的uiimage。短期ISH的回答,因为这是在我所提到的nschat:
UIImage *imageFromImageRef = [UIImage imageWithCGImage: base64ImageRef];
找出你接收的图像格式以及它的尺寸(宽度和高度,以像素为单位)。
return newI
你提到了在聊天,只是直argb8数据,所以记住。
UIImage *newImage = [[UIImage alloc] initWithData:nsdataFromBase64String];
你提到了在聊天,只是直argb8数据,所以记住。我不知道你收到的其他信息,如果在所有。
最佳答案 (Best Answer)
Short-ish answer, since this is mostly just going over what I mentioned in NSChat:
Figure out what the format of the image you're receiving is as well as its size (width and height, in pixels). You mentioned in chat that it's just straight ARGB8 data, so keep that in mind. I'm not sure how you're receiving the other info, if at all.
Using CGImageCreate, create a new image using what you know about the image already (i.e., presumably you know its width, height, and so on — if you don't, you should be packing this in with the image you're sending). E.g., this bundle of boilerplate that nobody likes to write:
// NOTE: have not tested if this even compiles -- consider it pseudocode.
CFDataRef bridgedD
CGDataProviderRef dataP
CGColorSpaceRef colorS
CGBitmapInfo infoFlags = kCGImageAlphaF // ARGB
// Get a color space
colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
// Assuming the decoded data is only pixel data
bridgedData
= (__bridge CFDataRef)decodedD
dataProvider = CGDataProviderCreateWithCFData(bridgedData);
// Given size_t width, height which you should already have somehow
image = CGImageCreate(
width, height, /* bpc */ 8, /* bpp */ 32, /* pitch */ width * 4,
colorSpace, infoFlags,
dataProvider, /* decode array */ NULL, /* interpolate? */ TRUE,
kCGRenderingIntentDefault /* adjust intent according to use */
// Release things the image took ownership of.
CGDataProviderRelease(dataProvider);
CGColorSpaceRelease(colorSpace);
That code's written with the idea that it's guaranteed to be ARGB_8888, the data is correct, nothing could possibly return NULL, etc. Copy/pasting the above code could potentially cause everything in a three mile radius to explode. Error handling's up to you (e.g., CGColorSpaceCreateWithName can potentially return null).
Allocate a UIImage using the CGImage. Since the UIImage will take ownership of/copy the CGImage, release your CGImageRef (actually, the docs say nothing about what UIImage does with the CGImage, but you're not going to use it anymore, so you must release yours).
使用cgimagecreate,创建一个新的图像使用你所了解的形象(即,你大概知道它的宽度,高度,等等,如果你不,你应该包装在你发送的图像)。使用cgimagecreate,创建一个新的图像使用你所了解的形象(即,你大概知道它的宽度,高度,等等,如果你不,你应该包装在你发送的图像)。例如,这束的样板,没人喜欢写:代码写的想法,这是保证是argb_8888,数据是正确的,什么都不可能返回null,等。复制/粘贴上面的代码可能会导致爆炸半径一三英里的一切。代码写的想法,这是保证是argb_8888,数据是正确的,什么都不可能返回null,等。复制/粘贴上面的代码可能会导致爆炸半径一三英里的一切。错误处理是你的(例如,cgcolorspacecreatewithname可能返回null)。// NOTE: have not tested if this even compiles -- consider it pseudocode.
CFDataRef bridgedD
CGDataProviderRef dataP
CGColorSpaceRef colorS
CGBitmapInfo infoFlags = kCGImageAlphaF // ARGB
// Get a color space
colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
// Assuming the decoded data is only pixel data
bridgedData
= (__bridge CFDataRef)decodedD
dataProvider = CGDataProviderCreateWithCFData(bridgedData);
// Given size_t width, height which you should already have somehow
image = CGImageCreate(
width, height, /* bpc */ 8, /* bpp */ 32, /* pitch */ width * 4,
colorSpace, infoFlags,
dataProvider, /* decode array */ NULL, /* interpolate? */ TRUE,
kCGRenderingIntentDefault /* adjust intent according to use */
// Release things the image took ownership of.
CGDataProviderRelease(dataProvider);
CGColorSpaceRelease(colorSpace);
分配一个uiimage使用cgimage。分配一个uiimage使用cgimage。由于uiimage将/复制cgimage所有权,释放你的cgimageref(实际上,医生什么也不说什么什么uiimage与cgimage,但是你不会再使用它,所以你必须释放你的)。
本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:objective c - How to convert CFDataRef into NSData? - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
J it only takes a minute:
How to convert CFDataRef into NSData ?
I have CFDataRef and I want to convert it into NSData. I am not getting how to do this.
anyone help me..
CGDataProviderRef dataProvider = CGImageGetDataProvider(screenShot);
CFDataRef data = CGDataProviderCopyData(dataProvider);
CGDataProviderRelease(dataProvider);
NSString *imagePathLocation = [destinationPath stringByAppendingString:[@"/" stringByAppendingString:DesktopImage]];
[[NSFileManager defaultManager]createFileAtPath: imagePathLocation contents:nil attributes:nil];
fileHandleForImage = [NSFileHandle fileHandleForWritingAtPath:imagePathLocation];
NSData * data1 = (NSData *)
[fileHandleForImage writeData:data1];
When I typecast(NSData * data1 = (NSData *)data) it Image get corrupted.
30.9k869110
It's toll-free bridged, so you just have to cast it.
NSData *myData = (NSData *)myCFDataR
11.1k33338
As @KrisMarkel said, you just have to transfer ownership to ARC like so:
Like so to transfer ownership:
NSData* data1 = (__bridge_transfer NSData*)
else to use the reference without transferring ownership:
NSData* data2 = (__bridge NSData*)
NSData *imageData = UIImagePNGRepresentation(originalImage);
CFDataRef cfdata = CFDataCreate(NULL, [imageData bytes], [imageData length]);
33.3k1176129
2,17121624
NSData *nsD
CFDataRef cfdata = CFDataCreate(NULL, [nsData bytes], [nsData length]);
to working example click .
2,17121624
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
rev .25236
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 cgimageref nsdata 的文章

 

随机推荐