我在一个应用程序中工作,我需要将 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/