将NSDictionary转换为NSData。

12

我有一个应用程序,可以拍照并将其上传到服务器。将其编码为base64并通过XMLRPC传递给我的PHP服务器。

我想获取从UIImagePickerController委托返回的NSDictionary信息。

-(void) imagePickerController:(UIImagePickerController *)imagePicker didFinishPickingMediaWithInfo:(NSDictionary *)info

我需要将其转换为NSData以进行编码。

那么,我该如何将NSDictionary转换为NSData?

5个回答

24
你可以使用一个NSKeyedArchiver将你的NSDictionary序列化为NSData对象。需要注意的是,字典中的所有对象都必须可序列化(在其继承树中某个时刻实现了NSCoding协议)才能使它正常工作。

懒得查找我的项目并复制代码,这里提供一些互联网上的代码:

编码

NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:yourDictionary forKey:@"Some Key Value"];
[archiver finishEncoding];
[archiver release];
/** data is ready now, and you can use it **/
[data release];

解码:

NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSDictionary *myDictionary = [[unarchiver decodeObjectForKey:@"Some Key Value"] retain];
[unarchiver finishDecoding];
[unarchiver release];
[data release];

我尝试实现这个,但是当我尝试对其进行编码时,我一直收到一个错误:2011-08-30 15:46:18.468 Satshot[1986:307] -[UIImage encodeWithCoder:]: unrecognized selector sent to instance 0x630de50 2011-08-30 15:46:18.502 Satshot[1986:307] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage encodeWithCoder:]: unrecognized selector sent to instance 0x630de50'信息是我从UIImagePickerController委托那里得到的,它应该是一个NSDictionary。我还尝试了[archiver encodeObject:info]并获得相同的错误。 - Padin215
错误发生在 [archiver encodeRootObject:info]; 这一行,而且是一个“SIGABRT”错误。抱歉之前忘记提到了。 - Padin215
2
UIImage不能直接通过NSKeyedArchiver进行序列化,需要通过一些变通方法。 - Perception

5

NSDictionary -> NSData:

    NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];

NSData -> NSDictionary:

    NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];

4
NSPropertyListSerialization 类可以让您最好地控制属性列表的写入和读取:
NSDictionary *dictionary = @{@"Hello" : @"World"};
NSData *data = [NSPropertyListSerialization dataWithPropertyList:dictionary
                                            format:NSPropertyListBinaryFormat_v1_0
                                            options:0
                                            error:NULL];

阅读:

NSData *data = ...
NSPropertyListFormat *format;
NSDictionary *dictionary = [NSPropertyListSerialization propertyListWithData:data
                                                        options:0
                                                        format:&format
                                                        error:NULL];

1
读取不正确... NSError *错误; NSPropertyListFormat 格式; NSDictionary * 字典 = [NSPropertyListSerialization propertyListWithData:self.inputFormEncodedDictionary options:0 format:&格式 error:&错误]; - Peter Lapisu

4

我知道有点晚了,但以防万一有人遇到同样的问题。 UIImage 不可序列化,但您可以使用以下代码将其序列化:

如果您的图像是 JPG

NSData *imagenBinaria = [NSData dataWithData:UIImageJPEGRepresentation(imagen, 0.0)]; 

// imagen is a UIImage object

如果您的图片是PNG格式:

NSData *imagenBinaria = [NSData dataWithData:UIImagePNGRepresentation(imagen)]; 

// imagen is a  UIImage object

3

在这个问题上,我想到了三个选项,另外两个在其他答案中提到的是NSKeyedArchiver和PropertyList,还有一个是在简单测试中给我提供了最紧凑数据的NSJSONSerialization。

NSDictionary *dictionary = @{@"message":@"Message from a cool guy", @"flag":@1};
NSData *prettyJson = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];
NSData *compactJson = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil];
NSData *plist = [NSPropertyListSerialization dataWithPropertyList:dictionary
                                                           format:NSPropertyListBinaryFormat_v1_0
                                                          options:0
                                                            error:NULL];
NSData *archived = [NSKeyedArchiver archivedDataWithRootObject:dictionary];`

不同方法的大小结果从最小到最大排序如下:

  • compactJson 46字节
  • prettyJson 57字节
  • plist 91字节
  • archived 316字节

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接