由于七牛没有批量上传的方法,找了很多博客,最终完成了批量上传,发篇文章记录一下
借鉴了这位朋友的大部分代码 http://www.jianshu.com/p/cdca6bb059de
TMUploadImageTools.h实现
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <QiniuSDK.h>
@interface TMUploadImageTools : NSObject
//获取七牛上传token
+ (void)getQiniuUploadToken:(void(^)(NSString*token))success failure:(void(^)())failure;
//单张图片上传
+ (void)uploadImage:(UIImage *)image progress: (QNUpProgressHandler)progress success:(void(^)(NSString*url))success failure:(void(^)())failure;
//多张图片上传
+ (void)uploadImages:(NSArray*)imageArray progress:(void(^)(CGFloat))progress success:(void(^)(NSArray*))success failure:(void(^)())failure;
@end
TMUploadImageTools.m实现
#import "TMUploadImageTools.h"
#import <AFNetworking.h>
#import <AFNetworkActivityIndicatorManager.h>
#import "TMQiNiuUploadHelper.h"
#define QiNiuBaseUrl @"http://img.tanrice.com.cn"
#define QiNiuToken @"ImeNlm03dPkxxBN6JugeFbVQYwaC6TFOsRB-V0F0:4-0UN6C2E1uYYwomG42-P3S_mpI=:eyJzY29wZSI6ImNvbmZpZ2RtIiwiZGVhZGxpbmUiOjE0OTIwNzI3MjJ9"
@implementation TMUploadImageTools
//获取当前时间
+ (NSString *)getDateTimeString{
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd"];
dateString = [formatter stringFromDate:[NSDate date]];
return dateString;
}
//获取随机字符串,用于生成文件名称
+ (NSString*)randomStringWithLength:(int)len{
NSString * letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
NSMutableString * randomString = [NSMutableString stringWithCapacity:len];
for(int i = 0; i < len; i++){
[randomString appendFormat:@"%C", [letters characterAtIndex:arc4random_uniform((int)[letters length])]];
}
return randomString;
}
+ (void)uploadImage:(UIImage *)image progress:(QNUpProgressHandler)progress success:(void (^)(NSString *))success failure:(void (^)())failure{
{
NSData *data = UIImageJPEGRepresentation(image, 0.01);
if (!data) {
if (failure) {
failure();
}
return;
}
//生成文件名称,调用上面的时间函数,和随机字符串函数
NSString *fileName = [NSString stringWithFormat:@"%@_%@.png",[TMUploadImageTools getDateTimeString],[TMUploadImageTools randomStringWithLength:8]];
QNUploadOption *option = [[QNUploadOption alloc]initWithMime:nil progressHandler:progress params:nil checkCrc:NO cancellationSignal:nil];
QNUploadManager *manager = [QNUploadManager sharedInstanceWithConfiguration:nil];
[manager putData:data key:fileName token:QiNiuToken complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
if (info.statusCode == 200 && resp) {
//成功后返回图片URL
NSString *url = [NSString stringWithFormat:@"%@/%@",QiNiuBaseUrl,resp[@"key"]];
if (success) {
success(url);
}
}else{
if (failure) {
failure();
}
}
} option:option];
}
}
+ (void)uploadImages:(NSArray*)imageArray progress:(void(^)(CGFloat))progress success:(void(^)(NSArray *))success failure:(void(^)())failure{
NSMutableArray * array = [[NSMutableArray alloc]init];
__block NSUInteger currentIndex = 0;
TMQiNiuUploadHelper * uploadHelper = [TMQiNiuUploadHelper sharedUploadHelper];
__weak typeof(uploadHelper) weakHelper = uploadHelper;
uploadHelper.singleFailureBlock= ^() {
failure();
return;
};
uploadHelper.singleSuccessBlock= ^(NSString *url) {
[array addObject:url];
currentIndex++;
if([array count] == [imageArray count]) {
success([array copy]);
return;
}else{
[TMUploadImageTools uploadImage:imageArray[currentIndex] progress:nil success:weakHelper.singleSuccessBlock failure:weakHelper.singleFailureBlock];
}
};
[TMUploadImageTools uploadImage:imageArray[0] progress:nil success:weakHelper.singleSuccessBlock failure:weakHelper.singleFailureBlock];
}
+ (void)getQiniuUploadToken:(void (^)(NSString *))success failure:(void (^)())failure{
//在这个方法中使用自己的网络工具类获取token
}
TMQiNiuUploadHelper.h
#import <Foundation/Foundation.h>
@interface TMQiNiuUploadHelper : NSObject
@property(copy,nonatomic) void(^singleSuccessBlock)(NSString*);
@property(copy,nonatomic) void(^singleFailureBlock)();
+ (instancetype)sharedUploadHelper;
@end
TMQiNiuUploadHelper.m
#import "TMQiNiuUploadHelper.h"
@implementation TMQiNiuUploadHelper
static id _instance = nil;
+ (id)allocWithZone:(struct _NSZone *)zone {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance= [super allocWithZone:zone];
});
return _instance;
}
+ (instancetype)sharedUploadHelper {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance= [[self alloc]init];
});
return _instance;
}
- (id)copyWithZone:(NSZone*)zone {
return _instance;
}
@end
以上的QiNiuBaseUrl是在七牛后台获取,像后台开发人员要,我这个是测试用的
QiNiuToken 也是要通过网络获取,通过调用自己的网络工具类来动态获取token;我这个token是写死的,token会过期,返回info.statusCode == 401。