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

标签 ios objective-c uiimageview afnetworking

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

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

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 个 UIImageView 发送 3 个不同的图像。这个程序是否可行,还是我需要通过不同的方法来工作?

有什么想法吗?

最佳答案

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

NSArray *myArrayOfImages = @[Image1,Image2,Image3]
NSArray *myArrayOfNames = @[strings..]
NSArray *myArrayOfFileNames = @[strings..]

然后在带有 block 参数的构造体中放置类似这样的东西..

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

您还可以使用字典或任何您想要的数据结构,关键是您只需循环并在构造 block 内追加。

关于ios - 将多个图像发送到 Web 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20780575/

相关文章:

ios - 如何在 iOS 应用程序中从 iCloud 自动删除数据备份

ios - 在UITableViewCell中以垂直顺序显示记录吗?

ios - 检查 UIImageView 是否在 subview 上

iphone - 如何在不推荐使用 didAccelerate 的情况下注意到快速前进的 Action ?

ios - 即使 useWebKit 设置为 true 也使用 UIWebView 进行博览会

ios - objective-C:为什么我的循环没有终止?

ios - AVAudioPlayer 控制句柄

iphone - UIImageView 带有大图像。问题

ios - 在 iOS 今天小部件中从 url 加载图像时出现内存错误

iOS - MPMoviePlayerController 无法从远程 URL 播放视频