5

I used the PickerController and loaded a few images on to a NSMutableArray.

Now i need to upload all of these images at once. am using AFNetworking and how can i do this?

I went through the AFNetworking documentation and there was a section called Creating an Upload Task for a Multi-Part Request, with Progress. However, i am not able to upload the images that are in my NSMutableArray.

**** NB: I want to upload the images in the NSMutableArray as a Byte Array. How can i do this? ****

The code i have so far,

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"site.com/upload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
Krunal
  • 77,632
  • 48
  • 245
  • 261
Illep
  • 16,375
  • 46
  • 171
  • 302
  • Can you zip the images and send them over internet ? – Sandeep May 12 '15 at 05:05
  • Multipart request is an excellent way to do this. The only trick is how to get the `NSData` of the picked images. Personally, I'd advise against `UIImagePNGRepresentation` or `UIImageJPEGRepresentation` and get the actual bytes of the original asset (see http://stackoverflow.com/a/27709329/1271826). Then you upload using https://github.com/AFNetworking/AFNetworking#post-multi-part-request except with `appendPartWithFileData:name:fileName:mimeType:error:`. Clearly, all of this assumes you've got a web service designed to parse multipart requests... – Rob May 12 '15 at 05:21
  • Should i loop `[formData appendPartWithFileData .....` several times to upload multiple files? – Illep May 12 '15 at 05:26
  • Yep, that is a fine way to tackle it. BTW, though, you might want to think about whether you really want one huge request (because if you have failure in the middle of huge request with many uploaded images, you'd have to start over) or separate requests for each image. The latter requires a more complicated design, but might be a little more fault tolerant. But that's up to you. – Rob May 12 '15 at 05:28

5 Answers5

6
UIImage *image1 = [UIImage imageNamed:@"about_app"];
UIImage *image2 = [UIImage imageNamed:@"alter"];
NSArray *array = @[image1,image2];
NSMutableURLRequest *request = [[AFNetWorkSingleton shareInstance] multipartFormRequestWithMethod:@"POST" path:@"Mindex/getimg" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>formData){
int i = 0;
for(UIImage *eachImage in array)
{
    NSData *imageData = UIImageJPEGRepresentation(eachImage,0.5);
    [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"file%d",i ] fileName:[NSString stringWithFormat:@"file%d.jpg",i ] mimeType:@"image/jpeg"];
    i++;
}
}];

Try this.

Hardik Mamtora
  • 1,642
  • 17
  • 23
2
-(void)uploadImages{

// image.finalImage - is image itself
// totalCount - Total number of images need to upload on server

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSDictionary *parameters = @{@"Key":@"Value",@"Key":@"Value"};
[manager POST:serverURL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:UIImagePNGRepresentation(image.finalImage) name:@"ImageName" fileName:[[Helper getRandomString:8] stringByAppendingString:@".png"] mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:(NSData *)responseObject options:kNilOptions error:nil];
    _uploadCounter+=1;
    if(_uploadCounter<totalCount){
        [self uploadImages];
    }else {
        NSLog(@"Uploading all images done");
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error");
}];

}

Try this, I have uploaded 10 images on server using this code and its successfully uploaded on sever.

Urmi
  • 344
  • 1
  • 14
  • @Priya i think Hardik's answer is good enough. you can check search specific methods in Obj-c only. – Urmi May 26 '17 at 17:58
  • which import file u use for AFHTTPRequestOperationManager for this. @Urmi – Hamza Imran Aug 02 '17 at 09:50
  • @HamzaImran Hey, I used AFNetworking Framework. so please check this framework you have to import "Afnetworking.h" file. – Urmi Aug 02 '17 at 19:51
1

If you want to upload multiple images and you want to keep the parameter name same for all the images you do it as below :

NSDictionary *parameters = @{@"user_key": @"*****"};
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:queryStringss parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    NSError *error;
    [formData appendPartWithFileData:imageData name:@"photo_file[0]" fileName:@"Picture44.png" mimeType:@"image/png"];
    [formData appendPartWithFileData:imageData1 name:@"photo_file[1]" fileName:@"Picture45.png" mimeType:@"image/png"];
} error:nil];

In this way the files would be sent as array to the server.

Ashish
  • 1,978
  • 1
  • 15
  • 7
1


Alamofire is nice source/library for the same:

Swift 4:

Alamofire.upload(
    multipartFormData: { multipartFormData in
        multipartFormData.append(unicornImageURL, withName: "unicorn")
        multipartFormData.append(rainbowImageURL, withName: "rainbow")
    },
    to: "https://httpbin.org/post",
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                debugPrint(response)
            }
        case .failure(let encodingError):
            print(encodingError)
        }
    }
)
Krunal
  • 77,632
  • 48
  • 245
  • 261
0

Common technique for turning a UIImage into NSData is with one of the following two methods.

NSData *pngData = UIImagePNGRepresentation(myUIImage);
CGFloat quality = 1.0; // ranges from 0 to 1, 0% to 100%
NSData *jpgData = UIImageJPEGRepresentation(myUIImage, quality);

This of course assumes you know which type your image is. But since you used a PickerController to fetch the images, then it should be fairly easy to determine this. You just pull out the value under the key UIImagePickerControllerReferenceURL and check the ext parameter. This comment shows it visually https://stackoverflow.com/a/11506499/1529638.

Community
  • 1
  • 1
JoshA
  • 133
  • 1
  • 11
  • Thanks for your answer. But how can i upload it using AFNetworking? I want to send a Byte Array. – Illep May 12 '15 at 05:17
  • What type of body are you supposed to send? Multi-part form data? – JoshA May 12 '15 at 05:19
  • You don't send "byte array", per se. You send `NSData`. See https://github.com/AFNetworking/AFNetworking#post-multi-part-request. – Rob May 12 '15 at 05:22