一尘不染

使用AFNetworking上传多张图像

swift

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

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

我浏览了AFNetworking文档,有一个名为的部分Creating an Upload Task for a Multi-Part Request, with Progress。但是,我无法上传中的图像NSMutableArray

*注意:我想以NSMutableArrayByte Array 的形式上传图像。我怎样才能做到这一点?*

我到目前为止的代码,

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

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

阅读 296

收藏
2020-07-07

共1个答案

一尘不染

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

试试这个。

2020-07-07