如何通过填充NSDictionary以JSON格式发送UIImage [英] How to send UIImage in JSON format, by filling a NSDictionary
问题描述
我正在尝试使用JSON将数据发送到服务器. 我可以使用对象和关键参数创建我的NSDictionary. 但是我想发送图片,图片是UIImage.
I'm trying to send data to a server with JSON. I am able to create my NSDictionary with my objects and key parameters. But I want to send my picture, and the picture is UIImage.
NSDictionary* mainJSON = [NSDictionary dictionaryWithObjectsAndKeys:
@"John",
@"First_Name",
@"McCintosh",
@"Last_name",
<HERE I WANT PICTURE>,
@"Profile_picture",
nil];
// Here I convert to NSDATA
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:mainJSON options:NSJSONWritingPrettyPrinted error:&error];
// Sending operation :
dispatch_async(kBgQueue, ^
{
NSData * data = [NSData dataWithContentsOfURL:@"addresSERVER"];
[self performSelectorOnMainThread:@selector(receivedResponseFromServer:)
withObject:data
waitUntilDone:YES];
}
);
所以我想知道如何在NSDictionary中添加图片? 因为我要发送图片的内容.如果我添加对象UIImage ...,我会正确发送整个对象吗?
So I'm wondering how can I add my picture in my NSDictionary? Because I want to send the content of my picture. If I add my object UIImage... I'll send the whole object right?
谢谢
推荐答案
您应将UIImage转换为NSString.使用名为NSDataAdditions的NSData类别.您可以在此处找到: NSDataAdditions类别
You should convert the UIImage to NSString. Use the category of NSData called NSDataAdditions.You can find here: NSDataAdditions category
使用方法:
//Convert an Image to String
UIImage *anImage;
NSString imageString = [UIImagePNGRepresentation(anImage) base64Encoding];
//To retrieve
NSData *data = [NSData dataWithBase64EncodedString:imageString];
UIImage *recoverImage = [[UIImage imageWithData:data];
这篇关于如何通过填充NSDictionary以JSON格式发送UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!