多張圖片進(jìn)行單張上傳

由于服務(wù)器設(shè)置的是只能單張圖片上傳,因此就出現(xiàn)了需要上傳多張圖片時的惡心情況。


??圖中看到的是只有這三行圖片,但其實(shí)是有八行的,每行圖片都是沒有限制的,因此我對這每行的圖片展示用一個scrollview進(jìn)行了封裝,這里不多介紹。其中圖片的傳入是有的行是必傳的,有的行是不必傳的。通過將每行的圖片放到一個數(shù)組中(利用for循環(huán)創(chuàng)建8個數(shù)組),然后將這八個數(shù)組放到一個大數(shù)組中。

一、使用遞歸算法進(jìn)行循環(huán)上傳操作

??開始進(jìn)入上傳圖片的代碼:

/**
 *  多組圖片單張上傳方法
 *
 *  @param up_ImgArr      圖片數(shù)組
 *  @param currentIndex   上傳下一張圖片的索引值,默認(rèn)為0,外部傳參為0即可
 *  @param urlStringArray 存放圖片上傳成功后返回的url值
 *  @param nextArrIndex   下一組圖片數(shù)組
 */

-(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是包含所有圖片數(shù)組的大數(shù)組中的第一個數(shù)組,然后就會進(jìn)行循環(huán)上傳,這種思想利用的是遞歸算法,直到執(zhí)行到自己想要的結(jié)果時需要return,如果遞歸中沒有return的話,那將進(jìn)入死循環(huán)狀態(tài),最終導(dǎo)致 程序崩潰。
上傳多張圖片邏輯規(guī)則:將數(shù)組中的每張圖片取出上傳完成之后再上傳下一張(拋去空數(shù)組),當(dāng)該數(shù)組上傳完畢之后進(jìn)行下一個數(shù)組進(jìn)行上傳,直到最后一個數(shù)組上傳完畢。

利用遞歸算法中寫的比較繞,如果仔細(xì)思考的話,其邏輯還是很清晰的。PS:這是本人廢了點(diǎn)腦細(xì)胞才寫的,實(shí)在是坑爹?。?!最后找到了使用dispatch_group來實(shí)現(xiàn)的就不那么惡心了,于是就有了??的這塊玉

二、使用dispatch_group和遞歸算法進(jìn)行多圖上傳

代碼如下:

/**
 *  多張圖片上傳方法
 *
 *  @param images 圖片數(shù)組
 *  @param urlArr 返回圖片數(shù)組對應(yīng)的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(@"上傳結(jié)束");
            return ;
        }
        
    });
}else{
    
    current_index++;
    
    if (current_index<self.imgsScrollView.count) {
        
        [self senderDataWithImages:self.imgsScrollView[current_index] urls:self.endImgArr[current_index]];
        
    }else{
        
        NSLog(@"上傳結(jié)束");
        return ;
    }
}
}

利用這個方法倒是減少了一部分腦細(xì)胞的損耗
demo地址如果喜歡的話給個star
ps:轉(zhuǎn)載請注明iOS小喬 http://www.itdecent.cn/p/ab2f0b7d764a

如果大家有更好的方法,請給我留言??

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容