美文网首页iOS开发技巧
[iOS] 单张图片上传 & 多长图片上传

[iOS] 单张图片上传 & 多长图片上传

作者: Leon_520 | 来源:发表于2017-01-12 21:24 被阅读591次

    前言:

    公司 App 目前需要增加多图上传的功能,但是后台只提供一个 单图上传接口,需要自己实现多图上传(以下代码都使用单图片上传接口)

    单图片上传

    以下是单图片上传代码: 单图片上传是根据 AFN封装调用的

    /**单图片上传 */
    + (void)upLoadSigleImage:(UIImage*) image completion:(void (^)(BOOL isSuccess))completion{
        if (!image) return;
        NSString * urlStr = @"/upload/uapi/upload/single";
        [[HttpTool sharedHttpTool] POST:urlStr parameters:nil  constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            [formData appendPartWithFileData:UIImagePNGRepresentation(image) name:@"file" fileName:@"default.png" mimeType:@"image/png"];
        } progress:nil success:^(NSURLSessionDataTask *_Nonnull task, id  _Nullable responseObject) {
            if ([responseObject[@"status"] integerValue] == 200) {
                completion(YES);
            }else{
                completion(NO);
            }
        } failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
            completion(NO);
        }];
    }
    

    多图上传

    为了实现多图片上传功能,我用了2种思路来进行实现,两种思路都需要使用单图片上传接口

    思路一: 串行异步, 图片一张一张进行上传,上传完一张,接着上传第二张,直到全部上传完毕.

    需要使用一个静态变量来记录之前上传状态,代码如下

    /**图片批量上传 -串行异步*/
    + (void)uploadMultiplePicturesWith:(NSArray<UIImage *> *) pictures
                            completion:(void(^)(BOOL isSuccess)) completion{
        if (pictures.count == 0) return;
        static NSInteger count = 0;
        
        if (count == [pictures count]) {
            count = 0;
            completion(YES);
            return;
        }
        [HttpTool upLoadSigleImage:pictures[count] completion:^(BOOL isSuccess) {
            if (isSuccess) {
                count++;
                [self uploadMultiplePicturesWith:pictures completion:completion];
            }else{
                completion(NO);
                return;
            }
        }];
    }
    

    思路二: 并发异步 多张图片同时进行异步上传操作,需要使用GCD dispatch_group来进行上传管理, 这种并发上传无法保证图片的上传顺序,因为是并发的, 顺序是随机的. 如果要保证图片上传的顺序,使用方式一

    代码如下

    /**图片批量上传 - 并发异步*/
    + (void)uploadMultiplePicturesWith:(NSArray<UIImage*>*) pictures
                            completion:(void(^)(BOOL isSuccess)) completion{
        
        if (pictures.count == 0) return;
        __block BOOL success = YES;
        
        dispatch_queue_t dispatchQueue = dispatch_get_global_queue(0, 0);
        dispatch_group_t dispatchGroup = dispatch_group_create();
        
        for (UIImage * pic in pictures) {
            dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
                dispatch_group_enter(dispatchGroup);
                [HttpTool upLoadSigleImage:pic completion:^(BOOL isSuccess) {
                    dispatch_group_leave(dispatchGroup);
                    if (!isSuccess) {
                        success = NO;
                    }
                }];
            });
        }
        dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{
            if (completion) {
                if (success) {
                    completion(YES);
                }else{
                    completion(NO);
                }
            }
        });
    }
    

    相关文章

      网友评论

        本文标题:[iOS] 单张图片上传 & 多长图片上传

        本文链接:https://www.haomeiwen.com/subject/jedjbttx.html