11

有一个可以拍照然后上传到服务器的应用程序。将它编码到64位并通过XMLRPC传递给我的PHP服务器。将nsdictionary转换为nsdata

我想采取由委托的UIImagePickerController

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

返回的NSDictionary中的信息并将其转换为NSData的,所以我可以编码。

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

回答

23

您可以使用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]; 
+0

我试着实现这一点,但我不断收到一个错误,当它ry编码它: 2011-08-30 15:46:18.468 Satshot [1986:307] - [UIImage encodeWithCoder:]:无法识别的选择器发送到实例0x630de50 2011-08-30 15:46:18 .502 Satshot [1986 :307 ***终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因是:“ - [UIImage的encodeWithCoder:]:无法识别的选择发送到实例0x630de50” 信息是我从委托的UIImagePickerController这应该是一个NSDictionary找回。我也试过[archiver encodeObject:info]并得到相同的错误。 – Padin215

+0

错误发生在[archiver encodeRootObject:info];和它的一个“SIGABRT”错误,因为忘了提到这一点。 – Padin215

+2

UIImage无法使用NSKeyedArchiver序列化,并非没有解决办法。 – Perception

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

它得到警告 –

+1

读取不正确... NSError *错误; NSPropertyListFormat格式; NSDictionary * dictionary = [NSPropertyListSerialization propertyListWithData:self.inputFormEncodedDictionary options:0 format:&format error:&error]; –

4

的NSDictionary - > NSData的:

NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary]; 

的NSData - >的NSDictionary:

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

三个选项想到我在此,其中2位在其他答案NSKeyedArchiver和PropertyList中,还有NSJSONSeriali在简单的测试中给了我最紧凑的数据。

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个字节
  • 存档的316个字节