//批量上传图片
- (void)upLoadImages{
dispatch_async(dispatch_get_main_queue(), ^{
//Loading-Start
[[NSNotificationCenter defaultCenter] postNotificationName:L_HUD_SHOW_NOTI object:nil];
});
__block BOOL upLoadSuccess = true;
[self.imageUrls removeAllObjects];
NSMutableArray * imgPathArray = @[self.cerIconImg,self.cerFrontImg,self.cerBackImg].mutableCopy;
dispatch_queue_t queue1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t groupQueue = dispatch_group_create();
for (int i = 0; i < imgPathArray.count; i++) {
__block NSString *tmpImgUrl;
if (upLoadSuccess) {
dispatch_group_enter(groupQueue);
id obj = imgPathArray[i];
if ([obj isKindOfClass:[UIImage class]]) {
UIImage *imgFile = obj;
dispatch_group_async(groupQueue, queue1, ^{
NSString *pic_url = [NSString stringWithFormat:@"%@",[kWebConfig postUrl:@"common/UPLOAD_FILEPATH"]];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSString *jwtToken = [AppSingleton shareSingleton].loginModel.accessToken ? :@"";
if (jwtToken.length>0) {
[manager.requestSerializer setValue:jwtToken forHTTPHeaderField:@"Authorization"];
}
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
//压缩
// NSData *fileData = [imgFile compressWithLengthLimit:50*1024];
NSData *fileData = UIImageJPEGRepresentation(imgFile, 0.5);
[manager POST:pic_url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
if (fileData != nil) {
[formData appendPartWithFileData:fileData name:@"fileData" fileName:@"icon.jpg" mimeType:@"image/jpeg"];
}
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSData *data =(NSData*)responseObject;
NSString *str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *obj = jsonToClass(str);
if ([self isRequestSuccess:obj]) {
NSDictionary *tmpDic = validateDictionary([obj objectForKey:@"dataInfo"]);
tmpImgUrl = validateString(tmpDic[@"fileId"]);
NSString *url = [NSString stringWithFormat:@"%@/%@",kWebConfig.prefixUrl,tmpImgUrl];
[self.imageUrls addObject:url];
NSLog(@"******上传成功*1");
} else{
upLoadSuccess = false;
NSLog(@"******上传失败*1");
}
dispatch_group_leave(groupQueue);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
upLoadSuccess = false;
dispatch_group_leave(groupQueue);
// [SGInfoAlert showInfo:@"图片提交网路错误"];
}];
});
} else{
NSLog(@"*****图片类型错误");
tmpImgUrl = (NSString *)obj;
[imgPathArray addObject:tmpImgUrl];
dispatch_group_leave(groupQueue);
}
}
}
dispatch_group_notify(groupQueue, queue1, ^{
dispatch_async(dispatch_get_main_queue(), ^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (_imageUrls.count==3) {
NSLog(@"*****图片上传完成");
[self requestMethod];
} else{
[SGInfoAlert showInfo:@"图片上传失败"];
//Loading-End
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hiddenHud) name:L_HUD_HIDDEN_NOTI object:nil];
}
});
});
});
/*{"returnCode":1000,"message":"上传成功","chainId":null,"dataInfo":{"url":"https://devcloud/658.jpg","downloadUrl":"","fileId":"group1/03/43/rI658.jpg","width":0,"height":0,"analyze":{"fuzzy":0,"food":0}}}
*/
}
//提交
- (void)requestMethod{
NSString *cerIconImgStr = _imageUrls[0];
NSString *cerFrontImgStr = _imageUrls[1];
NSString *cerBackImgStr = _imageUrls[2];
}