通过json将图像发送到服务器

时间:2022-11-25 21:19:28

I am posting data to a server from my ipad using json. But along with my data, i need to be able to send images aswell. I tried to add my image data to a dictionary and parse that into json, but json doesnt like nscfdata. What would be the easiest way i can post my images to the server? from other posts related to this topic, people have been converting to base64. Would i have to do that, or is there another easier or faster way? if i have to encode to base64 is there any tutorial on that?

我使用json从我的ipad向服务器发布数据。但随着我的数据,我需要能够发送图像。我试图将我的图像数据添加到字典并将其解析为json,但json不喜欢nscfdata。将图像发布到服务器的最简单方法是什么?从与此主题相关的其他帖子中,人们已经转换为base64。我是否必须这样做,还是有另一种更容易或更快的方式?如果我必须编码到base64有任何教程吗?

1 个解决方案

#1


6  

I convert it to base64. Check out this tutorial to get started!

我将其转换为base64。查看本教程即可开始使用!

http://www.cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html

Example:

NSImage *image = // some image
NSData *data = UIImagePNGRepresentation(image);
NSString *base64EncodedString = [data base64EncodedString];

You can then send the string via JSON.

然后,您可以通过JSON发送字符串。

#1


6  

I convert it to base64. Check out this tutorial to get started!

我将其转换为base64。查看本教程即可开始使用!

http://www.cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html

Example:

NSImage *image = // some image
NSData *data = UIImagePNGRepresentation(image);
NSString *base64EncodedString = [data base64EncodedString];

You can then send the string via JSON.

然后,您可以通过JSON发送字符串。