下面我來介紹一下如何使用OSS上傳圖片:
下載鏈接:下載鏈接
首先豆瘫,我們需要注冊賬號(hào)昵宇,拿到秘鑰:
//需要的一些基本信息
static NSString *const AccessKey = @"";
static NSString *const SecretKey = @"";
static NSString *const BucketName = @"";
static NSString *const AliYunHost = @"";
static NSString *kTempFolder = @"";
static NSString *rootUrl = @"";
下面是我封裝的幾個(gè)方法蒋譬,可以同步或異步,多張圖片或者單張圖片的上傳方法:
// 當(dāng)前上傳段長度腰湾、當(dāng)前已經(jīng)上傳總長度、一共需要上傳的總長度
typedef void(^HBUploadImageManageProgressBlock)(int64_t bytesSent, int64_t totalByteSent ,int64_t totalBytesExpectedToSend);
@interface HBUploadImageManage : NSObject
//異步上傳單張圖片
+ (void)asyncUploadImage:(UIImage *)image progress:(HBUploadImageManageProgressBlock)progressBlock complete:(void(^)(NSArray<NSString *> *names,UploadImageState state))complete;
//同步上傳單張圖片
+ (void)syncUploadImage:(UIImage *)image progress:(HBUploadImageManageProgressBlock)progressBlock complete:(void(^)(NSArray<NSString *> *names,UploadImageState state))complete;
//異步上傳多張圖片
+ (void)asyncUploadImages:(NSArray<UIImage *> *)images progress:(HBUploadImageManageProgressBlock)progressBlock complete:(void(^)(NSArray<NSString *> *names, UploadImageState state))complete;
//同步上傳多張圖片
+ (void)syncUploadImages:(NSArray<UIImage *> *)images progress:(HBUploadImageManageProgressBlock)progressBlock complete:(void(^)(NSArray<NSString *> *names, UploadImageState state))complete;
//異步單個(gè)上傳視頻
+ (void)asyncUploadVideo:(NSData *)data progress:(HBUploadImageManageProgressBlock)progressBlock complete:(void(^)(NSArray<NSString *> *names, UploadImageState state))complete;
//同步單個(gè)上傳視頻
+ (void)syncUploadVideo:(NSData *)data progress:(HBUploadImageManageProgressBlock)progressBlock complete:(void(^)(NSArray<NSString *> *names, UploadImageState state))complete;
設(shè)置回調(diào)類型:
typedef NS_ENUM(NSInteger, UploadImageState) {
UploadImageFailed = 0,
UploadImageSuccess = 1
};
下面介紹一下使用方法:
//上傳圖片
[HBUploadImageManage asyncUploadImage:img progress:^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
} complete:^(NSArray<NSString *> *names, UploadImageState state) {
if (state == UploadImageSuccess) {
NSLog(@"上傳默認(rèn)圖成功");
}else{
}
}];
上傳視頻
[self showLoadingInWindowWithMessage:@"上傳中..."];
[HBUploadImageManage asyncUploadVideo:self.videoData progress:^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
} complete:^(NSArray<NSString *> *names, UploadImageState state) {
[weakSelf dissmissWindowHud];
if (state == UploadImageSuccess) {
NSLog(@"上傳視頻成功");
weakSelf.customNavBar.rightButton.enabled = YES;
//有圖
if ([HBHuTool judgeArrayIsNotEmpty:names]) {
//拿到圖片
[weakSelf updateDefineVideoImageArray:names];
}else{
NSLog(@"上傳默認(rèn)圖失敗");
[weakSelf showInfoHudWithMessage:@"上傳失斔时稹省有!請重新上傳"] ;
}
}else{
weakSelf.customNavBar.rightButton.enabled = YES;
//上傳失敗
[weakSelf showInfoHudWithMessage:@"上傳失敳琛雪侥!請重新上傳"] ;
}
}];
如果對你有用仿粹,請給一個(gè)贊,你的贊是我分享的動(dòng)力昌罩!
END.