AliUpLoadImageTool.h:
#import <Foundation/Foundation.h>
@interface AliUpLoadImageTool : NSObject
/**
返回对象
@return AliUpLoadImageTool
*/
+ (AliUpLoadImageTool *)shareIncetance;
/**
这个方法用于上传单张图片到阿里云
@param parmar 这是参数是一个字典,直接将后台的字典传入
@param successBlock 上传成功后的回调,你可以在这里处理UI
@param faile 上传失败会走的回调
*/
+(void)upLoadImageWithPamgamar:(NSDictionary *)parmar imageData:(NSData *)imageData success:(void (^)(NSString *objectKey))successBlock faile:(void (^)(NSError *error))faile;
/**
这个方法用于上传多张图片到里云上
@param parmar 这是参数是一个字典,直接将后台的字典传入
@param successBlock 上传成功后的回调,你可以在这里处理UI
@param faile 上传失败会走的回调
*/
- (void)upLoadImageWithPamgamar:(NSDictionary *)parmar imageDataArray:(NSArray *)imageDataArray success:(void (^)(NSArray *objectKeys))successBlock faile:(void (^)(NSError *error))faile;
@end
AliUpLoadImageTool.m实现文件:
#import "AliUpLoadImageTool.h"
#import <AliyunOSSiOS/OSSService.h>
@interface AliUpLoadImageTool ()
@property (nonatomic,strong) NSMutableArray *dataSourece;
@end
@implementation AliUpLoadImageTool
+ (AliUpLoadImageTool *)shareIncetance {
static AliUpLoadImageTool *tool_;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
tool_ = [[AliUpLoadImageTool alloc] init];
});
return tool_;
}
- (NSMutableArray *)dataSourece {
if (!_dataSourece) {
_dataSourece = [[NSMutableArray alloc] init];
}
return _dataSourece;
}
+ (void)upLoadImageWithPamgamar:(NSDictionary *)parmar imageData:(NSData *)imageData success:(void (^)(NSString *objectKey))successBlock faile:(void (^)(NSError *error))faile {
NSString *endpoint = kAliEndPoint;
id<OSSCredentialProvider> credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey: kAiAccessKey secretKey:kAliAccessSecret];
OSSClientConfiguration * conf = [OSSClientConfiguration new];
conf.maxRetryCount = 2;
conf.timeoutIntervalForRequest = 30;
conf.timeoutIntervalForResource = 24 * 60 * 60;
OSSClient *client = [[OSSClient alloc] initWithEndpoint:[NSString stringWithFormat:@"http://%@",endpoint] credentialProvider:credential clientConfiguration:conf];
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
put.bucketName = kAliBucketName;
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval timeStamp =[dat timeIntervalSince1970]*1000;
NSString *objectKey = [NSString stringWithFormat:@"%@/%ld-%d.png",@"customer",(long)timeStamp + arc4random()%1000 +arc4random()%1000000,arc4random()%1000000];
put.objectKey =objectKey;
put.uploadingData = imageData; // 直接上传NSData
OSSTask * putTask = [client putObject:put];
// 上传阿里云
[putTask continueWithBlock:^id(OSSTask *task) {
dispatch_async(dispatch_get_main_queue(), ^{
if (!task.error) {
NSLog(@"url = %@%@",kAliImageURLPrefix,objectKey);
if (successBlock) {
successBlock(objectKey);
}
} else {
if (faile) {
faile(task.error);
}
}
});
return nil;
}];
}
/**
这个方法用于上传多张图片到里云上
@param imageDataArray 二进制图片数组
@param parmar 这是参数是一个字典,直接将后台的字典传入
@param successBlock 上传成功后的回调,你可以在这里处理UI
@param faile 上传失败会走的回调
*/
- (void)upLoadImageWithPamgamar:(NSDictionary *)parmar imageDataArray:(NSArray *)imageDataArray success:(void (^)(NSArray *objectKeys))successBlock faile:(void (^)(NSError *error))faile {
[self.dataSourece removeAllObjects];
NSString *endpoint = kAliEndPoint;
id<OSSCredentialProvider> credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:kAiAccessKey secretKey:kAliAccessSecret];
OSSClientConfiguration * conf = [OSSClientConfiguration new];
conf.maxRetryCount = 2;
conf.timeoutIntervalForRequest = 30;
conf.timeoutIntervalForResource = 24 * 60 * 60;
conf.maxConcurrentRequestCount = 10;
OSSClient *client = [[OSSClient alloc] initWithEndpoint:[NSString stringWithFormat:@"http://%@",endpoint] credentialProvider:credential clientConfiguration:conf];
for (NSInteger i= 0; i<imageDataArray.count ; i ++) {
[self.dataSourece addObject:@"notValue"];
[self uploadOneImage:[imageDataArray objectAtIndex:i] oSSClient:client currentIndex:i success:successBlock faile:faile];
}
}
- (void)uploadOneImage:(NSData *)imageData oSSClient:(OSSClient *)client currentIndex:(NSInteger)index success:(void (^)(NSArray *objectKeys))successBlock faile:(void (^)(NSError *))faile {
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
put.bucketName = kAliBucketName;
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval timeStamp =[dat timeIntervalSince1970]*1000;
NSString *objectKey = [NSString stringWithFormat:@"%@/%ld.png",@"customer",(long)timeStamp+10000*index];
put.objectKey =objectKey;
put.uploadingData = imageData; // 直接上传NSData
OSSTask * putTask = [client putObject:put];
// 上传阿里云
[putTask continueWithBlock:^id(OSSTask *task) {
task = [client presignPublicURLWithBucketName:kAliBucketName withObjectKey:objectKey];
dispatch_async(dispatch_get_main_queue(), ^{
if (!task.error) {
[self.dataSourece replaceObjectAtIndex:index withObject:[NSString stringWithFormat:@"%@%@",kAliImageURLPrefix,objectKey]];
if (![self.dataSourece containsObject:@"notValue"]) {
if (successBlock) {
successBlock(self.dataSourece);
}
}
} else {
[self.dataSourece removeAllObjects];
if (faile) {
faile(task.error);
}
}
});
return nil;
}];
}
代码中的一些key 相比如 :
#define kAiAccessKey @"xxx"
#define kAliAccessSecret @"xxxx"
#define kAliBucketName @"xxx-face"
#define kAliImageURLPrefix @"https://xxx.oss-cn-hangzhou.aliyuncs.com/"
#define kAliEndPoint @"oss-cn-hangzhou.aliyuncs.com"
这些key自己去找项目经理要,或者在阿里云开发者后台里面都可以找到,就不一一解释这么多了。
使用到的地方:
if (resultImageArr.count) {
// MARK: - 上传图片至阿里云
NSMutableArray *dataItems = [NSMutableArray new];
for (UIImage *faceImage in resultImageArr) {
[dataItems addObject:UIImageJPEGRepresentation(faceImage, 0.3)];
}
__block NSMutableArray *_imageUrls = [NSMutableArray new];
dispatch_queue_t queue = dispatch_queue_create("com.Chan.uploadImageToAliyunServer.www", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
[[AliUpLoadImageTool shareIncetance] upLoadImageWithPamgamar:nil imageDataArray:dataItems success:^(NSArray *objectKeys) {
//这里拿到的objectKeys就是图片在阿里云服务器上保存的位置地址URL,名字是用时间戳随机生成的(此处随自己意愿)
if (objectKeys && objectKeys.count) {
[_imageUrls addObject:objectKeys];
}
} faile:^(NSError *error) {
}];
});
dispatch_barrier_async(queue, ^{
NSLog(@"this is a barrier!");
});
dispatch_async(queue, ^{....
网友评论