send array of images with AFNetworkin ios

≯℡__Kan透↙ 提交于 2019-12-25 18:43:46

问题


I have an example, where I send image and parameters to server. But I need to send NSArray of images.How can I do this?

 NSData *imageToUpload = UIImageJPEGRepresentation(uploadedImgView.image, 1.0);//(uploadedImgView.image);
if (imageToUpload)
{
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:keyParameter, @"keyName", nil];

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://------"]];

NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"API name as you have" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData: imageToUpload name:@"image" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
     //NSLog(@"response: %@",jsons);

 }
                                 failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     if([operation.response statusCode] == 403)
     {
         //NSLog(@"Upload Failed");
         return;
     }
     //NSLog(@"error: %@", [operation error]);

 }];

[operation start];
}

Help me, please!


回答1:


I found an answer, I changed my code to this:

[formData appendPartWithFileData:data name:@"Photos[1]" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data name:@"Photos[2]" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];

and server understand it as array




回答2:


You can try this,

NSURL *url = [NSURL URLWithString:@URL_BASE];
int count = [imageArray count];

for(int i = 0; i < count; ++i)
 {
     UIImage* image = [imageArray objectAtIndex:i];
     AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
     NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:
                                             [NSNumber numberWithFloat: image.size.width], @"width",
                                             [NSNumber numberWithFloat: image.size.height], @"height",
                                          nil];

     NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@PATH_ALBUM_IMAGE_UPLOAD parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
             [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"file%d", i]  fileName:@"avatar.jpg" mimeyTpe:@"image/jpeg"];
         }];

     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
     [operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

             [progressView setProgress: totalBytesWritten*1.0f / totalBytesExpectedToWrite animated: YES];
             NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
             if(totalBytesWritten >= totalBytesExpectedToWrite)
             {
                 progressView.hidden = YES;
             }
         }];
     [operation start];
 }


来源:https://stackoverflow.com/questions/18096619/send-array-of-images-with-afnetworkin-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!