将多个图像发送到Web服务器

时间:2013-12-26 06:58:51

标签: ios objective-c uiimageview afnetworking

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

我有3 UIImageView从相机或相册中捕获图像数据。下面,我使用AFNetworking将1个图像发送到Web Server。

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];
}

}

我需要有人建议从3个UIImageViews发送3个不同的图像。是否可以使用此程序或我是否需要通过不同的方法工作?

任何想法?

1 个答案:

答案 0 :(得分:2)

您实际可以保留大部分代码。为什么不尝试将图像的所有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"];    
}

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