先上方法名,该方法提供了几个图片缩放的预处理功能,以及上传失败再次上传的功能。由于我们后台不支持修改图片名字,就去除了图片命名的功能。
用到的知识点主要是:
Http链接、多线程GCD
/**
上传多张图片
@param images 图片数组
@param url URL
@param paramsDic 参数
@param imageScale 上传图片缩放比例
@param times 上传失败-重新上传次数
@param uploadImageBlock 回调函数
*/
+(void)uploadImages:(NSArray<UIImage *> *)images url:(id)url params:(id)paramsDic imageScale:(CGFloat)imageScale reConnectTimes:(NSInteger)times finishBlock:(void (^)(NSArray<NSString*> *errorStrArr, NSArray<ResModel*> *modelArr))uploadImageBlock;
下面讲讲主要实现思路:
1.先对上传的URL路径进行合法性的判断;
2.用于回调的block赋值;
3.组装上传接口的传参,防止漏传参数;
4.生成NSURLRequest的对象,设置HTTPMethod、HTTPBody、TimeoutInterval、host等属性;
5.建立dispatch_group对象,用于管理请求(由于NSURLSessionDataTask创建时默认为异步的,为了加快上传的速度,通过dispatch_group管理这些请求),详见下面代码:
//图片请求结果
__block NSMutableArray* resModelResultArr = [[NSMutableArray alloc]init];
__block NSMutableArray* errorResultArr = [[NSMutableArray alloc]init];
//通过dispatch_group 管理多个请求
dispatch_queue_t dispatchQueue = dispatch_queue_create("uploadImageQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t dispatchGroup = dispatch_group_create();
for (int i = 0; i < images.count; i++) {
ResModel* model=[[ResModel alloc]init];
model.serviceStr=paramsDic[@"service"];
model.requestUrlStr=urlReq.URL.absoluteString;
model.requestStr=ccstr(@"%@%@",urlReq.URL.absoluteString,paraString);
dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
dispatch_group_enter(dispatchGroup);
NSURLRequest* request = [CC_UploadImagesTool recaculateImageDatas:images[i] imageScale:imageScale paramsDic:paramsDic request:urlReq];
[CC_UploadImagesTool requestSingleImageWithSession:session executorDelegate:executorDelegate request:request index:i+1 reConnectTimes:times model:model finishBlock:^(NSString *error, ResModel *resModel) {
if (error) {
[errorResultArr addObject:error];
}
[resModelResultArr addObject:resModel];
dispatch_group_leave(dispatchGroup);
}];
});
}
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
NSLog(@"所有图片上传完成-----end");
[session finishTasksAndInvalidate];
executorDelegate.finishUploadImagesCallbackBlock(errorResultArr, resModelResultArr);
});
这边将每个请求封装为单独的一个方法,方便请求失败时重新发起请求
+(void)requestSingleImageWithSession:(NSURLSession*)session executorDelegate:(CC_HttpTask *)executorDelegate request:(NSURLRequest*)request index:(int)index reConnectTimes:(NSInteger)reConnectTimes model:(ResModel*)model finishBlock:(void (^)(NSString *, ResModel *))block{
executorDelegate.finishCallbackBlock = block; // 绑定执行完成时的block
__block NSInteger reTryTimes = reConnectTimes;
__weak __typeof(self)weakSelf = self;
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
__strong __typeof(self)strongSelf = weakSelf;
if (error) {
//重新发起请求
if (reTryTimes == 0) {
[model parsingError:error];
executorDelegate.finishCallbackBlock(model.errorMsgStr, model);
}else{
NSLog(@"上传第%d张图片失败-----重连还剩%ld次", index, reTryTimes);
reTryTimes--;
[strongSelf requestSingleImageWithSession:session executorDelegate:executorDelegate request:request index:index reConnectTimes:reTryTimes model:model finishBlock:block];
}
}else{
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
model.resultDic = result;
NSLog(@"上传第%d张图片成功-----result:%@", index, result);
executorDelegate.finishCallbackBlock(model.errorMsgStr, model);
}
}];
[task resume];
}
下面对每个请求进行封装,通过数据流的方式上传数据,从而避免了不知道设什么mimeType
+(NSURLRequest*)recaculateImageDatas:(UIImage*)image imageScale:(CGFloat)imageScale paramsDic:(NSDictionary*)paramsDic request:(NSMutableURLRequest*)urlReq{
NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
//分界线 --AaB03x
NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
//结束符 AaB03x--
NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
//http body的字符串
NSMutableString *body=[[NSMutableString alloc]init];
//参数的集合的所有key的集合
NSArray *keys= [paramsDic allKeys];
//遍历keys
for(int i=0;i<[keys count];i++) {
//得到当前key
NSString *key=[keys objectAtIndex:i];
//添加分界线,换行
[body appendFormat:@"%@\r\n",MPboundary];
//添加字段名称,换2行
[body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key];
//添加字段的值
[body appendFormat:@"%@\r\n",[paramsDic objectForKey:key]];
}
//声明myRequestData,用来放入http body
NSMutableData *myRequestData=[NSMutableData data];
//将body字符串转化为UTF8格式的二进制
[myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
//要上传的图片--得到图片的data
NSData* data = UIImageJPEGRepresentation(image, imageScale);
NSMutableString *imgbody = [[NSMutableString alloc] init];
//添加分界线,换行
[imgbody appendFormat:@"%@\r\n",MPboundary];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat =@"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
[imgbody appendFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@\"\r\n", fileName];
//声明上传文件的格式
[imgbody appendFormat:@"Content-Type: application/octet-stream; charset=utf-8\r\n\r\n"];
//将body字符串转化为UTF8格式的二进制
[myRequestData appendData:[imgbody dataUsingEncoding:NSUTF8StringEncoding]];
//将image的data加入
[myRequestData appendData:data];
[myRequestData appendData:[ @"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//声明结束符:--AaB03x--
NSString *end=[[NSString alloc]initWithFormat:@"%@\r\n",endMPboundary];
//加入结束符--AaB03x--
[myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
//设置HTTPHeader中Content-Type的值
NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
//设置HTTPHeader
[urlReq setValue:content forHTTPHeaderField:@"Content-Type"];
//设置Content-Length
[urlReq setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[myRequestData length]] forHTTPHeaderField:@"Content-Length"];
//设置http body
[urlReq setHTTPBody:myRequestData];
return urlReq;
}
以上就是本功能实现的思路及代码。
遇到的坑:
由于我们后台一次数据流只支持单张图片上传,所以没贴多张的代码,有需要的小伙伴可以去这里
温馨提示: 上面代码是基于公司基础库实现的,所以可能非我们公司的小伙伴直接拿来用的话会报错,建议根据思路自己修改部分内容