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];
}
);
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?
Thanks
Okay, Thansk to @tolgamorf and @isaac, I tried using AFNetwork. I could do what I want. It's powerful and simple.
I took the code from official documentation from here .
Enjoy
You should convert the UIImage to NSString. Use the category of NSData called NSDataAdditions.You can find here: NSDataAdditions category
How to Use:
I wouldn't typically post an image using JSON. Although it is technically possible to encode an image into text, I don't think that's how JSON is intended to be used and I would personally avoid the practice.
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.
You can try send UIImage with NSString like this: Swift 3:
Objective-c:
This is Base64 format so you can decode this in any language