#import "AlyCommitImageTool.h"
#import <AliyunOSSiOS/OSSService.h>
@interface AlyCommitImageTool(){
NSMutableArray *imageArray;//圖片數(shù)組
NSMutableArray *imageNameArray;//圖片名字數(shù)組
NSMutableArray *imagePathArray;//圖片路徑數(shù)組
NSMutableArray *imagePutObjectArray;//圖片上傳數(shù)組
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:^{
//任務執(zhí)行
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;
}
//壓縮圖片處理(先調(diào)整分辨率)
-(NSData*)resetSizeOfImageData:(UIImage*)sourceImage maxSize:(NSInteger)maxSize
{
//優(yōu)先調(diào)整分辨率
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();
//再調(diào)整大小
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多線程上傳圖片
最后編輯于 :
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門竿报,熙熙樓的掌柜王于貴愁眉苦臉地迎上來铅乡,“玉大人,你說我怎么就攤上這事烈菌≌笮遥” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵芽世,是天一觀的道長挚赊。 經(jīng)常有香客問我,道長济瓢,這世上最難降的妖魔是什么荠割? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮旺矾,結果婚禮上蔑鹦,老公的妹妹穿的比我還像新娘。我一直安慰自己箕宙,他們只是感情好嚎朽,可當我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著扒吁,像睡著了一般火鼻。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上雕崩,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼肤寝!你這毒婦竟也來了当辐?” 一聲冷哼從身側響起,我...
- 正文 年R本政府宣布攒岛,位于F島的核電站赖临,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏灾锯。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一嗅榕、第九天 我趴在偏房一處隱蔽的房頂上張望顺饮。 院中可真熱鬧,春花似錦凌那、人聲如沸兼雄。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽赦肋。三九已至,卻和暖如春励稳,著一層夾襖步出監(jiān)牢的瞬間佃乘,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 這個是有個朋友問我上傳圖片到阿里云存儲OSS功能怎么做愁拭? 我看了很多文檔其實大概原理和email差不多枪汪, 主要你要...
- 前言 1.網(wǎng)絡請求 系統(tǒng)請求的方法 分為get請求和post請求1.1东帅、get請求系統(tǒng)提供了兩種方法,一種是Req...