将nsdictionary转换为nsdata [英] Convert nsdictionary to nsdata

查看:206
本文介绍了将nsdictionary转换为nsdata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

have an app that can take a picture and then upload to a server. encoding it to base 64 and pass it thru a XMLRPC to my php server.

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

i want to take the NSDictionary info that is returned from UIImagePickerController delegate

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

并将其转换为NSData,因此我可以对其进行编码。

and convert it to NSData so i can encode it.

所以,如何将NSDictionary转换为NSData?

so, how can i convert NSDictionary to an NSData?

推荐答案

您可以使用 NSKeyedArchiver 将NSDictionary序列化为NSData对象。请注意,字典中的所有对象都必须是可序列化的(在继承树的某个位置实现NSCoding)才能使其正常工作。

You can use an NSKeyedArchiver to serialize your NSDictionary to an NSData object. Note that all the objects in the dictionary will have to be serializable (implement NSCoding at some point in their inheritance tree) in order for this to work.

太懒了通过我的项目提升代码,所以这里有一些来自互联网:

Too lazy to go through my projects to lift code, so here is some from the Internet:

编码

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

这篇关于将nsdictionary转换为nsdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆