AFN上传多张图片代码:
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager]; sessionManager.requestSerializer = [AFHTTPRequestSerializer serializer]; // self.responseSerializer = [AFHTTPResponseSerializer serializer]; AFJSONResponseSerializer *response = [AFJSONResponseSerializer serializer]; response.removesKeysWithNullValues = YES; sessionManager.responseSerializer = response; sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",@"text/javascript",@"text/json",@"text/plain",nil]; NSDictionary *params = @{@"english_name":self.englishTextField.text, @"age":self.ageString, @"sex":self.sexString}; [sessionManager POST:[NSString stringWithFormat:@"%@/api/v1/personInfo/collect_info",BaseURL] parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { // [formData appendPartWithFileURL:[NSURL fileURLWithPath:self->fullPath] name:@"imageFile" fileName:@"currentImage.png" mimeType:@"image/png" error:nil]; // 上传文件 NSUInteger i = ; for (UIImage * image in self.imgArr) { //image的分类方法 UIImage * resizedImage = image; NSData * imgData = UIImageJPEGRepresentation(resizedImage, .); //拼接data
//name是服务器设置的接收图片文件的名字
//fileName:自己起的区别于其他图片的名字
//image/jpeg是服务器接收图片的格式
[formData appendPartWithFileData:imgData name:[NSString stringWithFormat:@"image"] fileName:@"image.png" mimeType:@"image/jpeg"]; i++; } } progress:^(NSProgress * _Nonnull uploadProgress) { // @property int64_t totalUnitCount; 需要下载文件的总大小 // @property int64_t completedUnitCount; 当前已经下载的大小 //打印下上传进度 NSLog(@"上传进度 %lf",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"请求成功:%@",responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { //请求失败 [GKMessageTool showError:@"请求失败"]; NSLog(@"请求失败:%@",error); }];