我正在尝试使用 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...我会发送整个对象,对吧?
Thanks
您应该将 UIImage 转换为 NSString。使用名为 NSDataAdditions 的 NSData 类别。您可以在这里找到:
如何使用:
//Convert an Image to String
UIImage *anImage;
NSString imageString = [UIImagePNGRepresentation(anImage) base64Encoding];
//To retrieve
NSData *data = [NSData dataWithBase64EncodedString:imageString];
UIImage *recoverImage = [[UIImage imageWithData:data];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)