多图上传,有很多方法.此方法基于AFN中AFHTTPSessionManager实现的多图上传
.h文件声明
/**
* 多图上传
*
* @param url 链接地址
* @param parameters 表单参数
* @param imageArray 图片数组UIImage
* @param imageArrayName 数组名称
* @param compressionQuality 压缩比例
* @param success 成功回调
* @param failure 失败回调
*
* @return NSURLSessionDataTask
*/
+ (NSURLSessionDataTask *)postMultipartImageWithURL:(NSString *)url
parameters:(id)parameters
imageArray:(NSArray *)imageArray
imageArrayName:(NSString *)imageArrayName
compressionQuality:(CGFloat)radio
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
.m实现文件
+ (NSURLSessionDataTask *)postMultipartImageWithURL:(NSString *)url
parameters:(id)parameters
imageArray:(NSArray *)imageArray
imageArrayName:(NSString *)imageArrayName
compressionQuality:(CGFloat)radio
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure {
for (int i =0; i < imageArray.count;i++) {
if (![imageArray[i]isKindOfClass:[UIImageclass]]) {
NSLog(@"请检查是否都是UIImage");
returnnil;
}
}
return [[AFAppDotNetAPIClientsharedClient]POST:urlparameters:parametersconstructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
int index =0;
//为每个图片生成名称
for (UIImage *imagein imageArray) {
NSDate *date = [NSDatedate];
NSDateFormatter *formatter = [[NSDateFormatteralloc]init];
formatter.dateFormat =@"yyyy-MM-dd";
NSString *dateStr = [formatterstringFromDate:date];
NSString *fileName = [NSStringstringWithFormat:@"%@%d.png",dateStr,index];
NSData *imageData = radio ==1.0f ?UIImageJPEGRepresentation(image,1.0f) :UIImageJPEGRepresentation(image, radio);;
[formData appendPartWithFileData:imageDataname:imageArrayNamefileName:fileNamemimeType:@"image/png/jpg"];
index ++;
}
} progress:^(NSProgress *_Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask *_Nonnull task,id _Nullable responseObject) {
success(responseObject);
} failure:^(NSURLSessionDataTask *_Nullable task,NSError *_Nonnull error) {
failure(error);
}];
}
注:其中AFAppDotNetAPIClient 为基于AFHTTPSessionManager的单例方法
具体实现如下:
staticNSString *const AFAppDotNetAPIBaseURLString = 公共部分;
+ (instancetype)sharedClient {
staticAFAppDotNetAPIClient *_sharedClient =nil;
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
;
_sharedClient = [[AFAppDotNetAPIClientalloc]initWithBaseURL:[NSURLURLWithString:AFAppDotNetAPIBaseURLString]];
_sharedClient.securityPolicy = [AFSecurityPolicypolicyWithPinningMode:AFSSLPinningModeNone];
// 数据下载完成,数据为JSON
_sharedClient.responseSerializer = [AFJSONResponseSerializerserializer];
// 数据为XML
// <NSXMLParser: 0x7f9ef5a4d240>
// _sharedClient.responseSerializer = [AFHTTPResponseSerializer serializer];
// 数据为PLIST
// _sharedClient.responseSerializer = [AFPropertyListResponseSerializer serializer];
// _sharedClient.responseSerializer = [AFXMLDocumentResponseSerializer serializer];
// AFHTTPResponseSerializer 返回原生的数据
// AFJSONResponseSerializer 数组或者是字典
// AFXMLParserResponseSerializer NSXMLParser对象
_sharedClient.responseSerializer.acceptableContentTypes=[NSSetsetWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html",@"text/plain",nil];
});
return _sharedClient;
}
更多详情请看 https://github.com/Ysiqian/AFNetworkRequest 基于AFN3.X的二次封装网络请求 支持POST GET 单图多图上传.可以在项目中直接用.觉得可以,各位看官可以star一下哦.谢谢.
有不对的地方请多多指出.谢谢!