七牛批量上传图片的封装处理

作者: 喵子G | 来源:发表于2017-09-01 15:39 被阅读559次

    1,工具类定义

    - (void)uploadImages:(NSArray<UIImage *> *)images success:(void(^)(NSArray *urls))success failure:(void(^)())failure;
    

    方法尽量简化参数个数,回调尽量方便调用者使用。只需要将需要上传的图片以数组的形式传过来,并用block的方式回调,类似于AFNetworking的网络请求。上传成功后,将图片的url以数据的形式在block中回调给调用者。

    工具类内部成员变量:

    @interface JKRQiNiuManager ()
    
    @property (nonatomic, strong) QNUploadManager *qnUploadManager;
    
    @end
    

    单子方法:

    + (instancetype)sharedManager {
        static JKRQiNiuManager *instance;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance = [[JKRQiNiuManager alloc] init];
        });
        return instance;
    }
    

    调用方法:

        [[JKRQiNiuManager sharedManager] uploadImages:imageArray success:^(NSArray *urls) {
            NSLog(@"上传成功");
        } failure:^{
            NSLog(@"上传失败");
        }];
    

    2,内部处理

    - (void)uploadImagess:(NSArray<UIImage *> *)images success:(void (^)(NSArray *urls))success failure:(void (^)())failure {
        if (images == nil || images.count == 0) failure();
        NSMutableArray *urls = [NSMutableArray array]; // 最终返回的图片url数组
        __block NSString *qiNiuToken = @""; // token
        dispatch_async(dispatch_get_global_queue(0, 0), ^{ // 异步处理上传方法
            dispatch_semaphore_t sem = dispatch_semaphore_create(0); // 利用信号量实现顺序上传
            for (UIImage *image in images) { // 遍历图片依次上传
                __block BOOL con; // 记录图片上传是否使用
                NSString *fileName = @""; // fileName
                NSString *domain = @"";   // domain
                [self qiNiuUploadImage:image token:qiNiuToken filename:fileName domain:domain  success:^(NSString *url) {
                    con = YES; // 上传成功
                    [urls addObject:url]; // 将url添加到结果数组
                    dispatch_semaphore_signal(sem);
                } failure:^{
                    con = NO; // 上传失败
                    dispatch_semaphore_signal(sem);
                }];
                dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
                if (!con) {
                    NSLog(@"上传失败");
                    failure(); // 上传失败回调
                    return;
                }
            }
            success(urls); // 所有图片上传成功后回调
        });
    }
    

    上传图片方法:

    - (void)qiNiuUploadImage:(UIImage *)image token:(NSString *)token filename:(NSString *)filename domain:(NSString *)domain success:(void (^)(NSString *url))success failure:(void (^)())failure {
        QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithMime:nil progressHandler:^(NSString *key, float percent) {
            NSLog(@"percent == %.2f", percent);
        } params:nil checkCrc:NO cancellationSignal:nil];
        [image jkr_compressToDataLength:300 * 1024 withBlock:^(NSData *data) { // 将图片压缩到300K以下
            [self.qnUploadManager putData:data key:filename token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
                if (info.ok) { // 上传成功
                    NSString *iconUrl = @""; // 处理url拼接逻辑
                    success(iconUrl);
                } else {   // 上传失败
                    failure();
                }
            } option:uploadOption];
        }];
    }
    

    相关文章

      网友评论

        本文标题:七牛批量上传图片的封装处理

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