我正在尝试使用 JSON 将数据发送到服务器。 我能够使用我的对象和关键参数创建我的 NSDictionary。 但是我要发我的图片,图片是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...我会发送整个对象吗?
谢谢
最佳答案
您应该将 UIImage 转换为 NSString。使用名为 NSDataAdditions 的 NSData 类别。您可以在此处找到: 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];
关于iphone - 如何通过填写 NSDictionary 以 JSON 格式发送 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16818662/