-(void)uploadImage{
_saveBtn.userInteractionEnabled=NO;
// 图片imagrUrl 站位
[_selectedPhotos enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[_imageIdArray addObject:[NSNull null]];
}];
dispatch_group_t group = dispatch_group_create();
[_selectedPhotos enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 进入线程组
dispatch_group_enter(group);
// 开启线程
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *url = [NSString stringWithFormat:@"%@",upLoadImgUrl];
NSString *userId = [NSString stringWithFormat:@"%@",[AppData share].userid];
NSDictionary *params = @{@"userid":userId,
@"type":@"12",
@"data":_selectedPhotos[idx]
};
NSString *token = [[NSUserDefaults standardUserDefaults] valueForKey:TOKEN];
NSString *aa = [NSString stringWithFormat:@"Bearer %@",token];
NSDictionary *headerDic = @{@"userid":userId,@"Authorization":aa};
UIImage *loadImg = _selectedPhotos[idx];
NSData *loadImgData = UIImageJPEGRepresentation(loadImg, 0.05);
[NTNetUtil uploadRequestWithUrl:url requestHeader:headerDic method:requestPOST parameters:params data:loadImgData block:^(NTHttpRequest *networkRequest, NSError *error) {
if (error == nil)
{
NSDictionary *responseDic = (NSDictionary *)networkRequest.responseData;
NSDictionary *dic = [NSDictionary dictionary];
NSString *r = [NSString stringWithFormat:@"%@",[responseDic objectForKey:@"success"]];
if ([r isEqualToString:@"1"]) {
dic = [responseDic objectForKey:@"data"];
NSString *imgUrl = [NSString stringWithFormat:@"%@",[dic objectForKey:@"url"]];
// 添加图片
@synchronized (self.imageIdArray)
{ // NSMutableArray 是线程不安全的,所以加个同步锁
[self.imageIdArray replaceObjectAtIndex:idx withObject:imgUrl];
NSLog(@"----------- 图片index-%ld 上传 ",idx);
}
}else {
}
}else {
}
// 离开线程组
dispatch_group_leave(group);
}];
});
}];
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"----------- 图片完成!");
for (NSString * response in self.imageIdArray) {
NSLog(@" 图片-- %@", response);
}
[self serialByGroupWait];
});
}
网友评论