#import "AlyCommitImageTool.h"
#import <AliyunOSSiOS/OSSService.h>
@interface AlyCommitImageTool(){
NSMutableArray *imageArray;//图片数组
NSMutableArray *imageNameArray;//图片名字数组
NSMutableArray *imagePathArray;//图片路径数组
NSMutableArray *imagePutObjectArray;//图片上传数组
NSInteger currentCommitIndex;
}
@property (strong, nonatomic) OSSClient *aliOssClient;
@end
@implementation AlyCommitImageTool
-(id)init{
if(self=[super init]){
imageArray=[[NSMutableArray alloc]init];
imageNameArray=[[NSMutableArray alloc]init];
imagePathArray=[[NSMutableArray alloc]init];
imagePutObjectArray=[[NSMutableArray alloc]init];
}
return self;
}
//阿里云 oss 上传于下载实体
- (OSSClient *)aliOssClient
{
if (!_aliOssClient) {
//判断服务端是否返回key和id
if(KgetUserValueByParaName(@"AccessKey")&& KgetUserValueByParaName(@"AccessId")){
id<OSSCredentialProvider> credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:KgetUserValueByParaName(@"AccessId") secretKey: KgetUserValueByParaName(@"AccessKey")];
_aliOssClient = [[OSSClient alloc] initWithEndpoint:OSS_ENDPOINT credentialProvider:credential];
}else{
id<OSSCredentialProvider> credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:OSS_ACCESSKEYID secretKey: OSS_ACCESSKEYSERCET];
_aliOssClient = [[OSSClient alloc] initWithEndpoint:OSS_ENDPOINT credentialProvider:credential];
}
}
return _aliOssClient;
}
- (void)uploadImages:(NSArray<UIImage *> *)images isAsync:(BOOL)isAsync fromWhere:(NSString *)path complete:(void(^)(NSArray<NSString *> *names, UploadImageState state))complete
{
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = images.count;
NSMutableArray *callBackNames = [NSMutableArray array];
int i = 0;
for (UIImage *image in images) {
if (image) {
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
//任务执行
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
put.bucketName = XinHuaAppDelegate.initialObj.UploadBucket;
// 路径格式:NSString *imageName =@"Party/2/110038/ClientUpload/2017/03/23/201703231234.jpg";
int aRandomNum = arc4random()%9996;
NSDate *now = [NSDate date];
NSLog(@"now date is: %@", now);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYYMMdd"];
NSString *dateStr = [formatter stringFromDate:[NSDate date]];
NSString *filePath = [NSString stringWithFormat:@"%@%d",dateStr,aRandomNum];
NSString *imageName =[NSString stringWithFormat:@"%@%@/%ld/%ld/%ld/%@.jpg",XinHuaAppDelegate.initialObj.UploadRoot,path,(long)[dateComponent year],(long)[dateComponent month],(long)[dateComponent day],filePath];
NSString *imageNamePath=imageName;
//如果第一个字符是/ 则去除
if([[imageName substringToIndex:1]isEqualToString:@"/"]){
imageName=[imageName substringFromIndex:1];
}else{
}
put.objectKey = imageName;
[callBackNames addObject:imageNamePath];
// NSData *data = UIImageJPEGRepresentation(image, 0.3);
NSData *data = [self imageData:image];
put.uploadingData = data;
OSSTask * putTask = [self.aliOssClient putObject:put];
[putTask waitUntilFinished]; // 阻塞直到上传完成
if (!putTask.error) {
NSLog(@"upload object success!");
} else {
NSLog(@"upload object failed, error: %@" , putTask.error);
}
if (isAsync) {
if (image == images.lastObject) {
NSLog(@"upload object finished!");
if (complete) {
complete([NSArray arrayWithArray:callBackNames] ,UploadImageSuccess);
}
}
}
}];
if (queue.operations.count != 0) {
[operation addDependency:queue.operations.lastObject];
}
[queue addOperation:operation];
}
i++;
}
if (!isAsync) {
[queue waitUntilAllOperationsAreFinished];
NSLog(@"haha");
if (complete) {
if (complete) {
complete([NSArray arrayWithArray:callBackNames], UploadImageSuccess);
}
}
}
}
-(NSData *)imageData:(UIImage *)myimage
{
NSData *data=UIImageJPEGRepresentation(myimage, 1.0);
if (data.length>100*1024) {
if (data.length>1024*1024) {//1M以及以上
// data = [self compressImage:myimage toMaxFileSize:512*1024/5];
data=[self resetSizeOfImageData:myimage maxSize:512*1024/5];
// data=UIImageJPEGRepresentation(myimage, 0.1);
}else if (data.length>512*1024) {//0.5M-1M
data=UIImageJPEGRepresentation(myimage, 0.5);
}else if (data.length>200*1024) {//0.25M-0.5M
data=UIImageJPEGRepresentation(myimage, 0.9);
}
}
return data;
}
//压缩图片处理(先调整分辨率)
-(NSData*)resetSizeOfImageData:(UIImage*)sourceImage maxSize:(NSInteger)maxSize
{
//优先调整分辨率
CGSize newSize=CGSizeMake(sourceImage.size.width, sourceImage.size.height);
CGFloat tempHeight=newSize.height/1024;
CGFloat tempWidth=newSize.width/1024;
// if (tempWidth>1.0&&tempWidth>tempHeight) {
newSize=CGSizeMake(sourceImage.size.width/tempWidth, sourceImage.size.height/tempHeight);
// }
UIGraphicsBeginImageContext(newSize);
[sourceImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//再调整大小
NSData *imageData=UIImageJPEGRepresentation(newImage, 0.4);
NSUInteger sizeOrigin=[imageData length];
NSUInteger sizeOriginKB=sizeOrigin/1024;
if (sizeOriginKB>maxSize) {
NSData *finalImageData=UIImageJPEGRepresentation(newImage, 0.5);
return finalImageData;
}
NSLog(@"#####***%lu",(unsigned long)imageData.length);
return imageData;
}
@end
ios 阿里云oss多线程上传图片
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 这个是有个朋友问我上传图片到阿里云存储OSS功能怎么做? 我看了很多文档其实大概原理和email差不多, 主要你要...
- 前言 1.网络请求 系统请求的方法 分为get请求和post请求1.1、get请求系统提供了两种方法,一种是Req...