Ios 向Web服务器发送多个图像

Ios 向Web服务器发送多个图像,ios,objective-c,uiimageview,afnetworking,Ios,Objective C,Uiimageview,Afnetworking,我在一个应用程序中工作,我需要向Web服务器发送3个图像。我不知道哪种完美的方法能快速有效地工作 我有3个UIImageView可以从相机或相册中捕获图像数据。下面,我使用AFNetworking向Web服务器发送1个图像 NSString *imgPath = [[NSBundle mainBundle]pathForResource:@"Default" ofType:@"png"]; NSData *imgData = UIImagePNGRepresentation([UIImage i

我在一个应用程序中工作,我需要向Web服务器发送3个图像。我不知道哪种完美的方法能快速有效地工作

我有3个
UIImageView
可以从相机或相册中捕获图像数据。下面,我使用
AFNetworking
向Web服务器发送1个图像

NSString *imgPath = [[NSBundle mainBundle]pathForResource:@"Default" ofType:@"png"];
NSData *imgData = UIImagePNGRepresentation([UIImage imageWithContentsOfFile:imgPath]);

NSData *imagVIewData = UIImageJPEGRepresentation(imageView1.image,90);
if (imagVIewData) {    
  AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://myurl.com/xxx.php]];

NSMutableURLRequest *myRequest =  [client multipartFormRequestWithMethod:@"POST" path:Nil parameters:Nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

  [formData appendPartWithFileData:imagVIewData name:@"file_upload" fileName:@"123.jpg" mimeType:@"images/jpeg"];
  AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:myRequest];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes",totalBytesWritten,totalBytesExpectedToWrite);
       }];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"upload complete");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@",operation.responseString);

}];

NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[queue addOperation:operation];
}

}
NSString*imgPath=[[NSBundle mainBundle]pathForResource:@“Default”of type:@“png”];
NSData*imgData=UIImagePNGRepresentation([UIImage-imageWithContentsOfFile:imgPath]);
NSData*imagVIewData=UIIMAGEJPEG表示法(imageView1.image,90);
if(imagVIewData){
AFHTTPClient*客户端=[AFHTTPClient客户端WithBaseURL:[NSURL URLWithString:@]http://myurl.com/xxx.php]];
NSMutableURLRequest*myRequest=[客户端multipartFormRequestWithMethod:@“POST”路径:Nil参数:Nil constructingBodyWithBlock:^(id formData){
[formData appendPartWithFileData:imagVIewData名称:@“file_upload”文件名:@“123.jpg”mimeType:@“images/jpeg”];
AFHTTPRequestOperation*操作=[[AFHTTPRequestOperation alloc]initWithRequest:myRequest];
[操作设置LoadProgressBlock:^(NSUTEGER字节写入、long long TotalBytesWrite、long long totalBytesExpectedToWrite){
NSLog(@“已发送%lld个字节,共%lld个字节”,TotalBytesWrite,totalBytesExpectedToWrite);
}];
[操作setCompletionBlockWithSuccess:^(AFHTTPRequestOperation*操作,id响应对象){
NSLog(@“上传完成”);
}失败:^(AFHTTPRequestOperation*操作,NSError*错误){
NSLog(@“%@”,操作响应日志);
}];
NSOperationQueue*队列=[[NSOperationQueue alloc]init];
[队列添加操作:操作];
}
}
我需要有人建议从3个UIImageView发送3个不同的图像。是否可以使用此程序或我需要通过不同的方法工作


有什么想法吗?

您拥有的大部分代码实际上都可以保留。为什么不尝试将图像的所有JPEG表示形式放入一个数组中呢

NSArray *myArrayOfImages = @[Image1,Image2,Image3]
NSArray *myArrayOfNames = @[strings..]
NSArray *myArrayOfFileNames = @[strings..]
然后在具有块参数的构造体中放入如下内容

for(int i=0; i < myArrayOfImages.length; i++){    
  NSData *temp = [myArrayOfImages objectAtIndex:i];    
  NSString *tempFile = [myArrayOfNames objectAtIndex:i]    
  NSString *tempFile = [myArrayOfFileNames objectAtIndex:i]    
  [formData appendPartWithFileData:temp name:tempName fileName:tempFile mimeType:@"images/jpeg"];    
}
for(inti=0;i

你也可以使用字典或任何你想要的数据结构,重点是你只需在构造块中循环和附加。

非常感谢Micheal,我今天会尝试,我会让你知道我的输出!没问题!我几天前自己几乎没搞清楚哈哈!