I'm working on a project where I need to add multiple NSDictionary
object's into an NSArray
and that NSArray
need to be send to server using the POST request . For this I'm using AFNetworking
framework . Well every thing worked fine till the time I came across a requirement of sending Image's
with object in the array .
I tried sending the image data as base64
string form , but the problem here is , when I tried sending the data this either take a lot of time and some time time's out and fails the connection . Well I tried with other way round of compressing the image and sending it but here the UIImage
quality is affected . So can any one help me to do this in a right way.
Thanks in advance !
Asked
Active
Viewed 793 times
1

SBK
- 11
- 6
2 Answers
0
NSData *imagedata = UIImagePNGRepresentation(_imgaddcontact.image);
NSString *base64 = [imagedata base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSMutableDictionary *parameterdiction = [NSMutableDictionary dictionary];
[parameterdiction setObject:base64 forKey:@"Photo"];
AFHTTPRequestOperation *op = [manager POST:@"<URL>" parameters:parameterdiction constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
if(imagedata != nil)
{
[formData appendPartWithFileData:imagedata name:@"Photo" fileName:@"image.png" mimeType:@"image/png"];
}
}

Purushothaman
- 106
- 1
- 1
- 6
-
Thanks for the input , here I have to send an array of object's. and not a single object , When I send a single object , it works perfectly , but when i try to send and array of object's including list of images , this gives me time out issue . I want to send all the NSDictionary object's with images in NSArray and send this array to the server .@purushothaman Here the the above answer we can only send one object and not a list of object .. Thanks once again for your help , and can you please let me know how will it work for sending an NSArray of objets which includes images . – SBK May 04 '16 at 07:36
-
please did you get a solution for this – Dhekra Zaied Jul 09 '17 at 15:00
0
You should use AFNetworking
. Drag and drop your library to your project and import AFNetworking.h
in your class and then you can do something like below,
// This request only depends on afnetworking
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:imageUploadUrl parameters:tempDict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// retrieve image by any way in my case below in your case whatever
UIImageView *tempImageView = (UIImageView*)[self.view viewWithTag:imageViewBaseTag+i];
UIImage *img = tempImageView.image;
NSData *imgData = UIImageJPEGRepresentation(img, 0.5); //convert image to data to send
[formData appendPartWithFileData:imgData name:[imageNameArray objectAtIndex:i] fileName:[imageNameArray objectAtIndex:i] mimeType:@"image/jpeg"]; // append image data to formdata to send server
} error:nil];
// This part is not depend on AFnetworking
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// handle your response here. you get respose in data parameter convert it in appropriate format like json or string and you can handle error also if occured
}]resume];
Hope this will help :)

marc_s
- 732,580
- 175
- 1,330
- 1,459

Ketan Parmar
- 27,092
- 9
- 50
- 75
-
Thanks for the input , here I have to send an array of object's. and not a single object of NSDictionary . – SBK May 04 '16 at 07:45
-
then you can pass that array of object instead of `tempDict` in `parameters` – Ketan Parmar May 04 '16 at 07:52
-
NSData *imgData = UIImageJPEGRepresentation(img, 0.5); is reducing the quality of the image . Can't we send the original image . – SBK May 04 '16 at 07:54
-
If you want to sen multiple images also then you can take for loop inside block. don't mix both thing. Parameter is different thing and image is different thing – Ketan Parmar May 04 '16 at 07:56
-
And i have post this code from my for loop so it is like so. you should manage according to your need – Ketan Parmar May 04 '16 at 07:57
-
`[imageNameArray objectAtIndex:i]` this is simply returns image name string – Ketan Parmar May 04 '16 at 07:58
-
and I guess there is a for loop for this line [formData appendPartWithFileData:imgData name:[imageNameArray objectAtIndex:i] fileName:[imageNameArray objectAtIndex:i] mimeType:@"image/jpeg"]; // append image data to formdata to send server – SBK May 04 '16 at 07:59
-
Yes in my case i have written in for loop so it is like so. you can use as per your requirement. – Ketan Parmar May 04 '16 at 09:38