iphone - 如何通过填写 NSDictionary 以 JSON 格式发送 UIImage

标签 iphone ios objective-c json xcode4

我正在尝试使用 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/

相关文章:

iphone - iPhone 上的乐谱。对示例代码有什么建议吗?

ios - 如何缩放应用程序,使 iPhone 6 与 iPhone 5 SpriteKit 相同(缩放)

iphone - 带有 -weak_library/usr/lib/libSystem.B.dylib 链接器标志的 objc_setAssociatedObject 上的 EXC_BAD_ACCESS

android - 是否有必要在我的 Android 设备上安装应用程序 camScanner

ios - 如何在初始化后更改 UIPickerView 中的行数

objective-c - 是否有用于 Objective-C Desktop Mac 应用程序的共享软件制作应用程序

iPhone - 使用 CALayers 而不是 UIView

objective-c - 你能通过 dismissModalViewControllerAnimated : or do you need a segue for the return journey? 返回数据吗

ios - 在堆栈 View 中的某些元素内设置空间

ios - 用于验证 API 的 OAuth2 - 无法重定向回我的应用程序