我使用OK
并将一些图片加载到PickerController
。
现在我需要一次上传所有这些图片。我正在使用NSMutableArray
,我该怎么做?
我浏览了AFNetworking
文档,其中有一个名为AFNetworking
的部分。但是,我无法上传Creating an Upload Task for a Multi-Part Request, with Progress
中的图片。
****注意:我想将NSMutableArray
中的图像作为字节数组上传。我怎样才能做到这一点? ****
我到目前为止的代码,
NSMutableArray
答案 0 :(得分:5)
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++;
}
}];
试试这个。
答案 1 :(得分:2)
-(void)uploadImages{
// image.finalImage - is image itself
// totalCount - Total number of images need to upload on server
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSDictionary *parameters = @{@"Key":@"Value",@"Key":@"Value"};
[manager POST:serverURL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:UIImagePNGRepresentation(image.finalImage) name:@"ImageName" fileName:[[Helper getRandomString:8] stringByAppendingString:@".png"] mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:(NSData *)responseObject options:kNilOptions error:nil];
_uploadCounter+=1;
if(_uploadCounter<totalCount){
[self uploadImages];
}else {
NSLog(@"Uploading all images done");
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error");
}];
}
试试这个,我使用此代码在服务器上上传了10张图片,并成功上传到服务器上。
答案 2 :(得分:1)
如果您想要上传多张图片,并希望保持所有图片的参数名称相同,请执行以下操作:
NSDictionary *parameters = @{@"user_key": @"*****"};
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:queryStringss parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSError *error;
[formData appendPartWithFileData:imageData name:@"photo_file[0]" fileName:@"Picture44.png" mimeType:@"image/png"];
[formData appendPartWithFileData:imageData1 name:@"photo_file[1]" fileName:@"Picture45.png" mimeType:@"image/png"];
} error:nil];
通过这种方式,文件将作为数组发送到服务器。
答案 3 :(得分:1)
Alamofire是相同的源/库:
Swift 4:
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(unicornImageURL, withName: "unicorn")
multipartFormData.append(rainbowImageURL, withName: "rainbow")
},
to: "https://httpbin.org/post",
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
}
)
答案 4 :(得分:0)
将UIImage
转换为NSData的常用技巧是使用以下两种方法之一。
NSData *pngData = UIImagePNGRepresentation(myUIImage);
CGFloat quality = 1.0; // ranges from 0 to 1, 0% to 100%
NSData *jpgData = UIImageJPEGRepresentation(myUIImage, quality);
这当然会假设您知道图像的类型。但由于您使用PickerController
来获取图像,因此确定这一点应该相当容易。您只需提取密钥UIImagePickerControllerReferenceURL
下的值并检查ext
参数即可。此评论以视觉方式显示https://stackoverflow.com/a/11506499/1529638。