使用AFNetworking上传多个图像 [英] Upload multiple images using AFNetworking

查看:104
本文介绍了使用AFNetworking上传多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 PickerController 并将一些图像加载到 NSMutableArray

I used the PickerController and loaded a few images on to a NSMutableArray.

现在我需要一次上传所有这些图片。我正在使用 AFNetworking 我该怎么办?

Now i need to upload all of these images at once. am using AFNetworking and how can i do this?

我经历了 AFNetworking 文档,有一个名为的部分为多部分请求创建上传任务,进度。但是,我无法上传我的 NSMutableArray 中的图像。

I went through the AFNetworking documentation and there was a section called Creating an Upload Task for a Multi-Part Request, with Progress. However, i am not able to upload the images that are in my NSMutableArray.

****注意:我想将 NSMutableArray 中的图像作为字节数组上传。我怎样才能做到这一点? ****

**** NB: I want to upload the images in the NSMutableArray as a Byte Array. How can i do this? ****

我到目前为止的代码,

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"site.com/upload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];


推荐答案

UIImage *image1 = [UIImage imageNamed:@"about_app"];
UIImage *image2 = [UIImage imageNamed:@"alter"];
NSArray *array = @[image1,image2];
NSMutableURLRequest *request = [[AFNetWorkSingleton shareInstance] multipartFormRequestWithMethod:@"POST" path:@"Mindex/getimg" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>formData){
int i = 0;
for(UIImage *eachImage in array)
{
    NSData *imageData = UIImageJPEGRepresentation(eachImage,0.5);
    [formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"file%d",i ] fileName:[NSString stringWithFormat:@"file%d.jpg",i ] mimeType:@"image/jpeg"];
    i++;
}
}];

试试这个。

这篇关于使用AFNetworking上传多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆