由于服务器设置的是只能单张图片上传,因此就出现了需要上传多张图片时的恶心情况。
👆图中看到的是只有这三行图片,但其实是有八行的,每行图片都是没有限制的,因此我对这每行的图片展示用一个scrollview进行了封装,这里不多介绍。其中图片的传入是有的行是必传的,有的行是不必传的。通过将每行的图片放到一个数组中(利用for循环创建8个数组),然后将这八个数组放到一个大数组中。
一、使用递归算法进行循环上传操作
👇开始进入上传图片的代码:
/**
* 多组图片单张上传方法
*
* @param up_ImgArr 图片数组
* @param currentIndex 上传下一张图片的索引值,默认为0,外部传参为0即可
* @param urlStringArray 存放图片上传成功后返回的url值
* @param nextArrIndex 下一组图片数组
*/
-(void)uploadImagesWithArray:(NSArray *)up_ImgArr currentIndex:(NSInteger)currentIndex imgUrlStringArray:(NSMutableArray *)urlStringArray nextArrayIndex:(NSInteger)nextArrIndex{
if (up_ImgArr.count>0) {
[API uploadImage:up_ImgArr[currentIndex] withPurpose:@"bang_album" andSuccessBlock:^(NSDictionary *dic) {
[urlStringArray addObject:dic[@"url"]];
if (currentIndex <up_ImgArr.count-1) {
NSInteger nextIndex = currentIndex+1;
//缩略图thumb , 大图 url
[self uploadImagesWithArray:up_ImgArr currentIndex:nextIndex imgUrlStringArray:urlStringArray nextArrayIndex:nextArrIndex];
}else{
NSInteger lastIndex = nextArrIndex+1;
if (lastIndex==self.imgsScrollView.count) {
[self uploadOtherData];
return ;
}else{
[self uploadImagesWithArray:self.imgsScrollView[lastIndex] currentIndex:0 imgUrlStringArray:self.endImgArr[lastIndex] nextArrayIndex:lastIndex];
}
}
} andFailBlock:^(NSError *error) {
NSLog(@"********%@",error);
}];
}else{
NSInteger lastIndex = nextArrIndex+1;
if (lastIndex==self.imgsScrollView.count) {
[self uploadOtherData];
return ;
}else{
[self uploadImagesWithArray:self.imgsScrollView[lastIndex] currentIndex:0 imgUrlStringArray:self.endImgArr[lastIndex] nextArrayIndex:lastIndex];
}
}
}
👆的代码中传入的up_ImgArr是包含所有图片数组的大数组中的第一个数组,然后就会进行循环上传,这种思想利用的是递归算法,直到执行到自己想要的结果时需要return,如果递归中没有return的话,那将进入死循环状态,最终导致 程序崩溃。
上传多张图片逻辑规则:将数组中的每张图片取出上传完成之后再上传下一张(抛去空数组),当该数组上传完毕之后进行下一个数组进行上传,直到最后一个数组上传完毕。
利用递归算法中写的比较绕,如果仔细思考的话,其逻辑还是很清晰的。PS:这是本人废了点脑细胞才写的,实在是坑爹啊!!最后找到了使用dispatch_group来实现的就不那么恶心了,于是就有了👇的这块玉
二、使用dispatch_group和递归算法进行多图上传
代码如下:
/**
* 多张图片上传方法
*
* @param images 图片数组
* @param urlArr 返回图片数组对应的url
*/
NSInteger current_index = 0;
-(void)senderDataWithImages:(NSArray *) images urls:(NSMutableArray *)urlArr{
if (images.count>0) {
dispatch_group_t group = dispatch_group_create();
for (UIImage *img in images) {
dispatch_group_enter(group);
[API uploadImage:img withPurpose:@"bang_album" andSuccessBlock:^(NSDictionary *dic) {
[urlArr addObject:dic[@"url"]];
dispatch_group_leave(group);
} andFailBlock:^(NSError *error) {
NSLog(@"********%@",error);
dispatch_group_leave(group);
}];
}
dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
current_index++;
if (current_index<self.imgsScrollView.count) {
[self senderDataWithImages:self.imgsScrollView[current_index] urls:self.endImgArr[current_index]];
}else{
NSLog(@"上传结束");
return ;
}
});
}else{
current_index++;
if (current_index<self.imgsScrollView.count) {
[self senderDataWithImages:self.imgsScrollView[current_index] urls:self.endImgArr[current_index]];
}else{
NSLog(@"上传结束");
return ;
}
}
}
利用这个方法倒是减少了一部分脑细胞的损耗
demo地址如果喜欢的话给个star
ps:转载请注明iOS小乔 http://www.jianshu.com/p/ab2f0b7d764a
如果大家有更好的方法,请给我留言😁
网友评论