七牛文件上传的过程,如下图所示
如果你不是很熟悉七牛SDK的集成过程,这里给您一点提示: cocoaPods podfile pod 'Qiniu' 引入七牛SDK文件.
过程如下:@1 获取上传文件到七牛的token,即用户权限。
@2 引用七牛SDK中文件方法,上传文件,上传成功和失败皆是有回调数据的。
QNUploadOption *opt = [[QNUploadOption alloc] initWithMime:nil progressHandler:progress params:nil checkCrc:NO cancellationSignal:nil];
QNUploadManager *uploadManager = [[QNUploadManager alloc]initWithRecorder:nil];
[uploadManager putData:data key:nil token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
if (info.statusCode == 200 && resp) {
NSString *url;
url = [NSString stringWithFormat:@"%@%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"token"], resp[@"key"]];
if (success) {
success(url);
}
} else {
if (failure) {
failure();
}
}
} option:opt];
下边给出批量上传的方法:
该类用于接收成功和失败的回调
#import
@interface QiNiuUploadHelper : NSObject
/**
* 成功回调
*/
@property (copy, nonatomic) void (^singleSuccessBlock)(NSString *);
/**
* 失败回调
*/
@property (copy, nonatomic) void (^singleFailureBlock)();
+ (instancetype)sharedInstance;
@end
.m实现方式
#import "QiNiuUploadHelper.h"
static QiNiuUploadHelper *_sharedInstance ;
@implementation QiNiuUploadHelper
+ (instancetype)sharedInstance {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[QiNiuUploadHelper alloc] init];
});
return _sharedInstance;
}
-(id)copyWithZone:(NSZone *)zone{
return _sharedInstance;
}
+(id)allocWithZone:(struct _NSZone *)zone{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [super allocWithZone:zone];
});
return _sharedInstance;
}
@end
批量上传的具体调用方法API
#import
#import
#import "QiniuSDK.h"
@interface QiNiuSystemService : NSObject
/**
* 上传图片
*
* @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;
/**
* 上传多张图片,按队列依次上传
*
* @param imageArray 存放image的数字
* @param progress 上传到七牛的进度
* @param success 成功block 返回key
* @param failure 失败block
*/
+ (void)uploadImages:(NSArray *)imageArray
progress:(void (^)(CGFloat))progress
success:(void (^)(NSArray *))success
failure:(void (^)())failure;
/**
* 获取七牛上传token
*
* @param success 成功block 返回token
* @param failure 失败block
*/
+ (void)qiNiuUploadToken:(void (^)(NSString *token))success failure:(void (^)())failure;
/**
* 获取七牛上传成功的key
*
* @param key 上传成功之后返回图片对应的key
* @param success 成功block 返回url地址
* @param failure 失败block
*/
+ (void)qiNiuUrlkey:(NSString *)key success:(void (^)(NSString *url))success failure:(void (^)())failure;
/**
* 获取七牛上传成功的成功后服务端返回的url
*
* @param keyArray 上传成功之后返回图片对应的key数组
* @param success 成功block 返回url地址数组
* @param failure 失败block
*/
+ (void)qiNiuUrlKeyArray:(NSArray *)keyArray success:(void (^)(NSArray *array))success failure:(void (^)())failure;
/**
* 将图片压缩为需要的比例
*
* @param image 需要处理的图片
* @param size 图片返回大小
*
* @return image
*/
+(UIImage*)originImage:(UIImage *)image scaleToSize:(CGSize)size;
@end
具体的.m实现:
#import "QiNiuSystemService.h"
#import "QiNiuUploadHelper.h"
#import "HotelRequest.h"
#import "QiniuSDK.h"
@implementation QiNiuSystemService
+ (void)uploadImage:(UIImage *)image progress:(QNUpProgressHandler)progress success:(void (^)(NSString *url))success failure:(void (^)())failure{
[QiNiuSystemService getQiniuUploadToken:^(NSString *token) {
UIImage *sizedImage = [QiNiuSystemService OriginImage:image scaleToSize:CGSizeMake(1000, 1000) ];
NSData *data = UIImageJPEGRepresentation(sizedImage, 0.3);
if (!data) {
if (failure) {
failure();
}
return;
}
QNUploadOption *opt = [[QNUploadOption alloc] initWithMime:nil progressHandler:progress params:nil checkCrc:NO cancellationSignal:nil];
QNUploadManager *uploadManager = [[QNUploadManager alloc]initWithRecorder:nil];
[uploadManager putData:data key:nil token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
if (info.statusCode == 200 && resp) {
NSString *url;
url = [NSString stringWithFormat:@"%@%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"token"], resp[@"key"]];
if (success) {
success(url);
}
}
else {
if (failure) {
failure();
}
}
} option:opt];
} failure:^{
if (failure) {
failure();
}
}];
}
//上传图片
+ (void)uploadImages:(NSArray *)imageArray progress:(void (^)(CGFloat))progress success:(void (^)(NSArray *))success failure:(void (^)())failure{
NSMutableArray *array = [[NSMutableArray alloc] init];
__block float totalProgress = 0.0f;
__block float partProgress = 1.0f / [imageArray count];
__block NSUInteger currentIndex = 0;
QiNiuUploadHelper *uploadHelper = [QiNiuUploadHelper sharedInstance];
__weak typeof(uploadHelper) weakHelper = uploadHelper;
uploadHelper.singleFailureBlock = ^() {
failure();
return;
};
uploadHelper.singleSuccessBlock = ^(NSString *url) {
[array addObject:url];
totalProgress += partProgress;
progress(totalProgress);
currentIndex++;
if ([array count] == [imageArray count]) {
success([array copy]);
return;
}
else {
[QiNiuSystemService uploadImage:imageArray[currentIndex] progress:nil success:weakHelper.singleSuccessBlock failure:weakHelper.singleFailureBlock];
}
};
[QiNiuSystemService uploadImage:imageArray[0] progress:nil success:weakHelper.singleSuccessBlock failure:weakHelper.singleFailureBlock];
}
//获取七牛的token
+ (void)qiNiuUploadToken:(void (^)(NSString *))success failure:(void (^)())failure{
[HotelRequest requestHotelPicwindinfoManager:[HttpRequest requestOperationManager]
success:^(id responseObject) {
NSString * token = [NSString stringWithFormat:@"%@",responseObject[@"picwindinfo"]];
if (success) {
success(token);
}
} failure:^(NSOperation *operation, NSError *error) {
if (failure) {
failure();
}
}];
}
// 获取七牛上传成功的key
+ (void)qiNiuUrlkey:(NSString *)key success:(void (^)(NSString *url))success failure:(void (^)())failure{
[HotelRequest requestHotelSubjectPicPathChangeWithPicpath:key
manager:[HttpRequest requestOperationManager]
success:^(id responseObject) {
NSString *picUrl = responseObject[@"picurl"];
if (success) {
success(picUrl);
}
} failure:^(NSOperation *operation, NSError *error) {
if (failure) {
failure();
}
}];
}
// 获取七牛上传成功的成功后服务端返回的url
+ (void)qiNiuUrlKeyArray:(NSArray *)keyArray success:(void (^)(NSArray *array))success failure:(void (^)())failure{
NSMutableArray *UrlArray = [[NSMutableArray alloc] init];
__block NSUInteger currentIndex = 0;
QiNiuUploadHelper *uploadHelper = [QiNiuUploadHelper sharedInstance];
__weak typeof(uploadHelper) weakHelper = uploadHelper;
uploadHelper.singleFailureBlock = ^() {
failure();
return;
};
uploadHelper.singleSuccessBlock = ^(NSString *url) {
[UrlArray addObject:url];
currentIndex++;
if ([UrlArray count] == [keyArray count]) {
success([UrlArray copy]);
return;
}
else {
[QiNiuSystemService getQiniuUrlkey:url success:weakHelper.singleSuccessBlock failure:weakHelper.singleFailureBlock];
}
};
[QiNiuSystemService getQiniuUrlkey:keyArray[0] success:weakHelper.singleSuccessBlock failure:weakHelper.singleFailureBlock];
}
//压缩上传图片的大小比例
+ (UIImage*)originImage:(UIImage *)image scaleToSize:(CGSize)size{
UIGraphicsBeginImageContext(size); //size 为CGSize类型,即你所需要的图片尺寸
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage; //返回的就是已经改变的图片
}