因客户要求,在APP中的图片都是从七牛的服务器中获取的,所以所有的上传图片也要上传到七牛上.又因为服务器不做批量上传的接口,所以只能自己写了;七牛的sdk只提供单张图片的上传,坑人!不多说了,上代码
UploadImageTool.h
#import <Foundation/Foundation.h>
#import <QiniuSDK.h>
#import <UIKit/UIKit.h>
@interface UploadImageTool :NSObject
//获取七牛上传token
+ (void)getQiniuUploadToken:(void(^)(NSString*token))success failure:(void(^)())failure;
/**
*上传图片
*
*@param image需要上传的image
*@param progress上传进度block
*@param success成功block返回url地址
*@param failure失败block
*/
+ (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
-------------------------------------------------------------------------------
UploadImageTool.m
#import"UploadImageTool.h"
#import"AFNetworking.h"
#import"AFNetworkActivityIndicatorManager.h"
#import"VCOApi.h"
#import"VCOAPIClient.h"
#import"QiniuUploadHelper.h"
#define QiNiuBaseUrl @"http://7xozpn.com2.z0.glb.qiniucdn.com/"
@implementationUploadImageTool
//给图片命名
+ (NSString*)getDateTimeString
{
NSDateFormatter*formatter;
NSString*dateString;
formatter = [[NSDateFormatteralloc]init];
[formattersetDateFormat:@"yyyy-MM-dd"];
dateString = [formatterstringFromDate:[NSDatedate]];
returndateString;
}
+ (NSString*)randomStringWithLength:(int)len
{
NSString*letters =@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
NSMutableString*randomString = [NSMutableStringstringWithCapacity: len];
for(inti=0; i
[randomStringappendFormat:@"%C", [letterscharacterAtIndex:arc4random_uniform((int)[letterslength])]];
}
returnrandomString;
}
//上传单张图片
+ (void)uploadImage:(UIImage*)image progress:(QNUpProgressHandler)progress success:(void(^)(NSString*url))success failure:(void(^)())failure {
[UploadImageToolgetQiniuUploadToken:^(NSString*token) {
NSData*data =UIImageJPEGRepresentation(image,0.01);
if(!data) {
if(failure) {
failure();
}
return;
}
NSString*fileName = [NSStringstringWithFormat:@"%@_%@.png", [UploadImageToolgetDateTimeString], [UploadImageToolrandomStringWithLength:8]];
QNUploadOption*opt = [[QNUploadOptionalloc]initWithMime:nil
progressHandler:progress
params:nil
checkCrc:NO
cancellationSignal:nil];
QNUploadManager*uploadManager = [QNUploadManager sharedInstanceWithConfiguration:nil];
[uploadManager putData:data
key:fileName
token:token
complete:^(QNResponseInfo*info,NSString*key,NSDictionary*resp) {
if(info.statusCode==200&& resp) {
NSString*url= [NSString stringWithFormat:@"%@%@",QiNiuBaseUrl, resp[@"key"]];
if(success) {
success(url);
}
}
else{
if(failure) {
failure();
}
}
}option:opt];
}failure:^{
}];
}
//上传多张图片
+ (void)uploadImages:(NSArray*)imageArray progress:(void(^)(CGFloat))progress success:(void(^)(NSArray*))success failure:(void(^)())failure
{
NSMutableArray*array = [[NSMutableArray alloc]init];
__blockCGFloattotalProgress =0.0f;
__blockCGFloatpartProgress =1.0f/ [imageArraycount];
__blockNSUIntegercurrentIndex =0;
QiniuUploadHelper*uploadHelper = [QiniuUploadHelper sharedUploadHelper];
__weaktypeof(uploadHelper) weakHelper = uploadHelper;
uploadHelper.singleFailureBlock= ^() {
failure();
return;
};
uploadHelper.singleSuccessBlock= ^(NSString*url) {
[array addObject:url];
totalProgress += partProgress;
progress(totalProgress);
currentIndex++;
if([arraycount] == [imageArraycount]) {
success([arraycopy]);
return;
}else{
NSLog(@"---%ld",currentIndex);
[UploadImageTool uploadImage:imageArray[currentIndex] progress:nil success:weakHelper.singleSuccessBlock failure:weakHelper.singleFailureBlock];
}
};
[UploadImageTool uploadImage:imageArray[0] progress:nil success:weakHelper.singleSuccessBlock failure:weakHelper.singleFailureBlock];
}
//获取七牛的token
+ (void)getQiniuUploadToken:(void(^)(NSString*))success failure:(void(^)())failure {
NSString*aPath = [NSStringstringWithFormat:@"%@%@getQiniuUpToken",BaseUrl,Url_info];
[[VCOAPIClient sharedClient] requestJsonDataWithPath:aPath withParams:nil withMethodType:Post andBlock:^(id data,NSError*error) {
if(data) {
if(success) {
success([data objectForKey:@"data"]);
}
}else{
if(failure) {
failure();
}
}
}];
}
@end
--------------------------------------------------------------------------------
QiniuUploadHelper.h
#import <Foundation/Foundation.h>
@interfaceQiniuUploadHelper :NSObject
@property(copy,nonatomic)void(^singleSuccessBlock)(NSString*);
@property(copy,nonatomic)void(^singleFailureBlock)();
+ (instancetype)sharedUploadHelper;
@end
--------------------------------------------------------------------------------
QiniuUploadHelper.m
#import"QiniuUploadHelper.h"
@implementation QiniuUploadHelper
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
--------------------------------------------------------------------------------
使用方法:
单张图片:
[UploadImageTool uploadImage:self.holdImage progress:nil success:^(NSString*url) {
NSLog(@"qin niu --%@",url);
}failure:^{
NSLog(@" --->> error:");
}];
多张图片:
[UploadImageTool uploadImages:self.imgArray progress:^(CGFloat progress) {
NSLog(@"qin niu --%f",progress);
}success:^(NSArray*urlArr) {
NSLog(@"qin niu --%@",urlArr);
}failure:^{
NSLog(@" --->> error:");
}];
demo:http://git.oschina.net/cdyiwhy/QiNiuUploadImage