iOS ucloud多图异步上传

本文介绍如何使用UCloud SDK结合GCD的group队列实现iOS应用中多图的异步上传,通过FBBasicUploadModel封装上传逻辑,包括上传状态、进度回调及错误处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近开发项目中,用到ucloud上传多图,使用GCD的group队列,进行多图异步上传,提高上传速度。调用如下:

//ucloud多图上传封装

- (void)uploadMultiPic

{

    NSMutableArray *uploadImageArr = [NSMutableArray array];

    //创建group

    dispatch_group_t uploadGroup = dispatch_group_create();

    

    for (int i = 0; i<_toUploadImageArr.count;i++) {

        FBAttachmentCellStyleModel *model = _toUploadImageArr[i];

        dispatch_group_enter(uploadGroup);

        [model uploadSuccess:^(id obj) {

            NSLog(@"上传图片成功:%d url:%@",i,obj);

            FBBasicUploadModel *model = [[FBBasicUploadModel alloc] init];

            model.imgUrl = obj;

            model.index = [NSString stringWithFormat:@"%d",i];

            [uploadImageArr addObject:model];

            dispatch_group_leave(uploadGroup);

        } progress:^(CGFloat p) {

            

        } failure:^(NSError *error) {

            dispatch_group_leave(uploadGroup);

        }];

    }

    

    if (_sortImageArray != nil) {

        [_sortImageArray removeAllObjects];

    }

    //全部上传完 整理图片数组

    @weakify(self);

    dispatch_group_notify(uploadGroup, dispatch_get_main_queue(), ^{

         [MBProgressHUD hideHUDForView:nil];

        @strongify(self)

        for (int i=0;i<uploadImageArr.count;i++) {

            for (FBBasicUploadModel *model in uploadImageArr) {

                if ([model.index intValue] == i) {

                    [self->_sortImageArray addObject:model.imgUrl];

                }

            }

        }

        //提交ucloud返回图片url数组给服务器

        [self publish];

    });

}

其中用到ucloud上传,做了一层封装,FBAttachmentCellStyleModel继承于FBBasicUploadModel,FBBasicUploadModel实现文件如下:

#import <UIKit/UIKit.h>

#import "UFileSDK.h"

 

/**

上传状态

*/

typedef NS_ENUM(NSInteger, YBAttachmentUploadStatus) {

    /**未上传*/

    YBAttachmentUploadStatusNone = 0,

    /**上传中*/

    YBAttachmentUploadStatusUploading,

    /**上传结束*/

    YBAttachmentUploadStatusEnd,

};

 

@interface FBBasicUploadModel : NSObject

@property (nonatomic, strong) UFileSDK*   ufilesdk;

 

@property (nonatomic, strong) NSString *imageName;

@property (nonatomic, strong) NSString *imagePath;

@property (nonatomic, strong) NSString *imageType;

 

 

/**上传成功后返回的url*/

@property (nonatomic, copy) NSString *imgUrl;

/**选择后的图片*/

@property (nonatomic, strong) UIImage *image;

/**上传状态*/

@property (nonatomic, assign) YBAttachmentUploadStatus uploadStatus;

/**进度0~1*/

@property (nonatomic, assign) CGFloat progress;

/**当前是第几张图片*/

@property (nonatomic, copy) NSString *index;

 

/**进度回调*/

@property (nonatomic, copy) void(^UploadProgress)(CGFloat p);

/**成功回调*/

@property (nonatomic, copy) void(^UploadSuccess)(id obj);

/**失败回调*/

@property (nonatomic, copy) void(^UploadFailure)(NSError *error);

 

 

/*!

@method

@brief ucloud上传图片

@param success 成功回调

@param imageProcess 图片上传进度回调

@param failure 失败回调

@discussion

*/

- (void)uploadSuccess:(void(^)(id obj))success

             progress:(void(^)(CGFloat p))imageProcess

              failure:(void(^)(NSError *error))failure;

 

@end

 

 

#import "UFileAPIUtils.h"

 

@implementation FBBasicUploadModel

 

- (UFileSDK *)ufilesdk

{

    if (_ufilesdk == nil) {

        _ufilesdk = [[UFileSDK alloc] initWith:bucket ProxySuffix:proxySuffix PublicKey:publicToken PrivateKey:privateToken];

    }

    return _ufilesdk;

}

- (void)uploadSuccess:(void(^)(id obj))success

             progress:(void(^)(CGFloat p))imageProcess

              failure:(void(^)(NSError *error))failure

{

 

    self.uploadStatus = YBAttachmentUploadStatusUploading;

    

    

    NSString*  strkey = [NSString stringWithFormat:@"%@.%@",self.imageName,self.imageType];

     NSString * strPath = [[NSBundle mainBundle] pathForResource:@"initscreen" ofType:@"jpg"];

    if (self.imagePath != nil) {

        strPath = self.imagePath;

    }

    else{

        strPath = [[NSBundle mainBundle] pathForResource:self.imageName ofType:self.imageType];

    }

   

    NSData*  contentData = [[NSData alloc] initWithContentsOfFile:strPath];

    

   // NSString* contentType = @"image/jpeg";

    NSString* contentType = [NSString stringWithFormat:@"image/%@",self.imageType];

    

    NSString* contentMD5 = [UFileAPIUtils calcMD5ForData:contentData];

    

    

    NSString* strAuth = [self.ufilesdk calcKey:@"PUT" Key:strkey MD5:contentMD5 ContentType:contentType CallBackPolicy:nil];

    

    //上传参与签名计算 要传authorization 生成的签名

    //上传参与签名计算 要传data 文件内容

    //上传参与签名计算 要传option 其他参数

    //option[kUFileSDKOptionFileType]=contentType 文件类型

    //option[kUFileSDKOptionMD5]=contentMD5 文件内容md5

    NSDictionary * option = @{kUFileSDKOptionFileType: contentType, kUFileSDKOptionMD5: contentMD5};

    

    [self.ufilesdk.ufileApi putFile:strkey

                      authorization:strAuth

                             option:option

                               data:contentData progress:^(NSProgress* progress){

                                   imageProcess(progress.fractionCompleted);

                               }

                            success:^(NSDictionary* response){

                                if(response)

                                {

                                    //自己拼接https://imguf.****.cn+图片名(包含图片格式) https://imguf.****.cn/initscreen.jpg

                                    NSString *imageUrl = [NSString stringWithFormat:@"%@%@",imageHost,strkey];

                                   // NSLog(@"image url:%@",imageUrl);

                                    success(imageUrl);

                                }

                            }

                            failure:^(NSError* error){

                                

                                NSString*erMsg = @"";

                                if (error && error.domain == kUFileSDKAPIErrorDomain) {

                                    erMsg = error.userInfo[@"ErrMsg"];

                                }

                                else

                                {

                                    erMsg = error.description;

                                }

                                failure(error);

                            }];

}

@end

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值