最近开发项目中,用到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