调用方法非常简单
只需要初始定义一个全局变量
XMWUploadManager * _uploadManager =[ [XMWUploadManager creat] initXMUploadManager];//初始化
上传图片调用方法
[_uploadManager uploadLocalImage:imgArr complete:^(NSMutableArray *imageUrlArr) {
//imageUrlArr就是返回的图片url数组
}];
下面是源码
.h文件
#import <Foundation/Foundation.h>
@interface XMWUploadManager : NSObject
+(XMWUploadManager*)creat;
- (void)initXMUploadManager;
//上传图片方法
- (void)uploadLocalImage:(NSArray*)imageArray complete:(void(^)(NSMutableArray* imageUrlArr))completed;
@end```
.m文件
import "XMWUploadManager.h"
import "TXYUploadManager.h"
@interface XMWUploadManager()
@property (nonatomic,strong) TXYUploadManager *uploadImageManager;
@property (nonatomic,copy) NSString * appId;
@property (nonatomic,copy) NSString * bucket;
@property (nonatomic,copy) NSString * persistenceId;
@property (nonatomic,copy) NSString * sign;
@property (nonatomic,strong) TXYUploadTask * uploadPhotoTask;
@property (nonatomic,strong) UIImage * upImage;
@end
@implementation XMWUploadManager
{
__block NSInteger _upPhotoNum;//总的上传数量
__block NSMutableArray * _urlArray;//保存上传过的图片的url的数组
}
- (XMWUploadManager*)creat{
return [[XMWUploadManager alloc]init];
}
- (instancetype)init{
if (self = [super init]) {
[self initXMUploadManager];
}
return self;
} - (void)initXMUploadManager{
_urlArray = [NSMutableArray array];
self.appId = @"appid写自己的";//腾讯云appID,创建的时候会返回给你
self.bucket = @"bucketname";//上传的仓库名字 自定义
self.persistenceId = @"persistenceId";//自定义,唯一,没有限制persistenceId
[self getJiandingCode:nil];
_uploadImageManager = [[TXYUploadManager alloc] initWithCloudType:TXYCloudTypeForImage
persistenceId:self.persistenceId
appId:self.appId];
}
-(void)getJiandingCode:(UIImage*)image{
[HttpRequest getWithURLString:SIGN_URL parameters:nil success:^(id responseObject) {
self.sign = [responseObject objectForKey:@"sign"];
if(image!=nil){
// [self uploadImage:image];
}
} failure:^(NSError *error) {
self.sign = nil;
}];
}
- (void)getSign:(void(^)(BOOL success))compled{
[HttpRequest getWithURLString:SIGN_URL parameters:nil success:^(id responseObject) {
self.sign = [responseObject objectForKey:@"sign"];
if (compled) {
compled(YES);
}
} failure:^(NSError *error) {
self.sign = nil;
if (compled) {
compled(NO);
}
}];
}
-
(void)uploadLocalImage:(NSArray)imageArray complete:(void(^)(NSMutableArray imageUrlArr))completed{
_upPhotoNum = imageArray.count;
if (![XMNullTextManager isXMBlank:self.sign]) {
for(int i = 0;i<imageArray.count;i++){
[self uploadImage:imageArray[i] complete:^(BOOL success, NSString *url) {
_upPhotoNum--;
if (success) {
[_urlArray addObject:url];
}
if (_upPhotoNum==0) {
if (completed) {
completed(_urlArray);
}
}
}];
}}else{
[self getSign:^(BOOL success) {for(int i = 0;i<imageArray.count;i++){ [self uploadImage:imageArray[i] complete:^(BOOL success, NSString *url) { _upPhotoNum--; if (success) { [_urlArray addObject:url]; } if (_upPhotoNum==0) { if (completed) { completed(_urlArray); } } }]; } }];
}
}
// 上传方法
-(void)uploadImage:(UIImage*)image complete:(void(^)(BOOL success, NSString * url))completed
{
self.uploadPhotoTask = [[TXYPhotoUploadTask alloc] initWithImageData: UIImageJPEGRepresentation(image, 1.0)
fileName:@"name"
sign:self.sign
bucket:self.bucket
expiredDate:0
msgContext:@"服务器透穿信息"
fileId:nil];
//[self showLoadingWithView:self];
//上传图片总共四步之 第五步: 上传
[_uploadImageManager upload:_uploadPhotoTask
complete:^(TXYTaskRsp *resp, NSDictionary *context) {
TXYPhotoUploadTaskRsp * photoTask = (TXYPhotoUploadTaskRsp *)resp;
if (resp.retCode>=0) {
//上传成功
if (completed) {
completed(YES,photoTask.photoURL);
}
}else{
//上传失败
if (completed) {
completed(NO,nil);
}
}
}
progress:^(int64_t totalSize, int64_t sendSize, NSDictionary *context) {
//上传的进度
}
stateChange:^(TXYUploadTaskState state, NSDictionary *context) {
switch (state) {
case TXYUploadTaskStateWait:
NSLog(@"任务等待中");
break;
case TXYUploadTaskStateConnecting:
NSLog(@"任务连接中");
break;
case TXYUploadTaskStateFail:
NSLog(@"任务失败");
[DejalBezelActivityView removeViewAnimated:YES];
break;
case TXYUploadTaskStateSuccess:
NSLog(@"任务成功");
[DejalBezelActivityView removeViewAnimated:YES];
break;
default:
break;
}}
];
}