使用AFNetworkin ios发送图像阵列
我有一个例子,我将图像和参数发送到服务器。但是我需要发送一系列的图片。我怎么做使用AFNetworkin ios发送图像阵列,ios,nsarray,afnetworking,Ios,Nsarray,Afnetworking,我有一个例子,我将图像和参数发送到服务器。但是我需要发送一系列的图片。我怎么做 NSData *imageToUpload = UIImageJPEGRepresentation(uploadedImgView.image, 1.0);//(uploadedImgView.image); if (imageToUpload) { NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:keyParameter,
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];
}
请帮帮我 你可以试试这个
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];
}
我找到了答案,我将代码更改为:
[formData appendPartWithFileData:data name:@"Photos[1]" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData:data name:@"Photos[2]" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
服务器将其理解为数组我理解您的代码可以做什么,但我需要发送一个NSArray!这是代码服务器请求的:“照片[]”:document.getElementByIdphoto.files[0]//还有其他参数。现在有什么问题?我需要在一个请求中发送所有照片这是只发送图像。如果我们必须为每个图像发送额外的参数,该怎么办?i、 例如,数组中的每个对象都是带有参数位置、图像id、图像实际图像、宽度等的字典,例如:图像数组={firstName=Kate;has\u image=1;lastName=Bell;图像=recordID=1;}@Revanth您找到解决方案了吗?意味着为每个图像添加额外的参数?