5

我正在尝试使用 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... 我会发送整个对象对吗?

谢谢

4

4 回答 4

8

You should convert the UIImage to NSString. Use the category of NSData called NSDataAdditions.You can find here: NSDataAdditions category

How to Use:

//Convert an Image to String
UIImage *anImage;
NSString imageString = [UIImagePNGRepresentation(anImage) base64Encoding];

//To retrieve
NSData *data = [NSData dataWithBase64EncodedString:imageString];
UIImage *recoverImage = [[UIImage imageWithData:data];
于 2013-05-29T18:18:52.333 回答
4

我通常不会使用 JSON 发布图像。尽管在技术上可以将图像编码为文本,但我认为 JSON 的用途并非如此,我个人会避免这种做法。

Deal with images as NSData. That's what they are. There are tons of examples online that illustrate how to do this.

One common approach is to upload an image to a web server, then take the URL of the uploaded image and add that to your JSON dictionary, such that your submitted JSON dictionary carries a string representing the URL of the image to download -- not the image itself.

于 2013-05-29T16:13:36.633 回答
2

您可以尝试使用 NSString 发送 UIImage,如下所示:Swift 3:

if let jpegData = UIImageJPEGRepresentation(image, 1.0) {
    var encodedString = jpegData.base64EncodedString()
    var mainJSON = [
        "First_Name" : "John",
        "Last_name" : "McCintosh",
        "Profile_picture" : encodedString
    ]

}

目标-c:

NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
NSString *encodedString = [imageData base64Encoding];


 NSDictionary* mainJSON = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"John",
                          @"First_Name",
                          @"McCintosh",
                          @"Last_name",
                          encodedString,
                          @"Profile_picture",
                          nil];

这是 Base64 格式,因此您可以用任何语言对其进行解码

于 2013-05-29T16:02:14.893 回答
0

Okay, Thansk to @tolgamorf and @isaac, I tried using AFNetwork. I could do what I want. It's powerful and simple.

NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST"  path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg"  mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[httpClient enqueueHTTPRequestOperation:operation];

I took the code from official documentation from here .

Enjoy

于 2013-05-30T15:49:58.887 回答