多张图片上传
2017-07-26 本文已影响72人
为之则易ing
多张图片上传,可判断哪张上传成功或者失败,以及所有上传完成的回调
- (void)uploadTaskWithImage:(UIImage*)image success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure{
// 构造 NSURLRequest
// Request *request = [self requestWithImage:imageTmp url:R_UploadImage_do params:dict filename:nil name:@"myFile" showHUDMessage:nil];
NSString *uploadUrl = [R_BASE_URL stringByAppendingString:R_UploadImage_do];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"financing",@"fileDirectory", nil];
[dict setObject:GlobalData.loginInfo.token forKey:@"token"];
[dict setObject:GlobalData.loginInfo.intermediaryId forKey:@"intermediaryId"];
AFHTTPSessionManager *manager = [RequestHandler sharedInstance].manager;
manager.requestSerializer.timeoutInterval = 5 * 60;
[manager POST:uploadUrl parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSData *imageData = UIImageJPEGRepresentation(image, 0.4);
NSLog(@"____%ld",imageData.length);//481300\349595
NSString *imageFileName = nil;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
imageFileName = [NSString stringWithFormat:@"%@.jpg", str];
[formData appendPartWithFileData:imageData name:@"myFile" fileName:imageFileName mimeType:@"image/jpeg"];
// UIImage *imag = [UIImage imageNamed:@"limite"];
// NSData *da = UIImageJPEGRepresentation(imag, 0.8);
// [formData appendPartWithFileData:da name:name fileName:@"1" mimeType:@"image/jpeg"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"****____%f",(double)uploadProgress.completedUnitCount/uploadProgress.totalUnitCount);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
success(task,responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(task,error);
}];
}
- (void)runDispatchTest:(id)sender {
// 需要上传的数据
NSArray *imageArray = @[@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4",@"updateAppBackImage",@"pic1",@"pic2",@"pic3",@"pic4"];
NSMutableArray *images = [NSMutableArray array];
NSMutableArray* result = [NSMutableArray array];
NSLog(@"%ld",imageArray.count);
for (NSString *img in imageArray) {
[images addObject:[UIImage imageNamed:img]];
[result addObject:[NSNull null]];
}
dispatch_group_t group = dispatch_group_create();
for (NSInteger i = 0; i < images.count; i++) {
dispatch_group_enter(group);
[self uploadTaskWithImage:images[i] success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"第 %d 张图片上传成功: %@", (int)i + 1, @"");
@synchronized (result) {
result[i] = responseObject;
}
dispatch_group_leave(group);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"第 %d 张图片上传失败: %@", (int)i + 1, error);
dispatch_group_leave(group);
}];
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"上传完成!");
for (id response in result) {
NSLog(@"%@", response);
}
});
}