阿里云上传图片

+ (void)uploadImages:(NSArray *)images isAsync:(BOOL)isAsync complete:(void(^)(NSArray *names, UploadImageState state))complete

{

id credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:AccessKeysecretKey:SecretKey];

OSSClient*client = [[OSSClient alloc] initWithEndpoint:AliYunHost credentialProvider:credential];

NSOperationQueue*queue = [[NSOperationQueuealloc]init];

queue.maxConcurrentOperationCount= images.count;

NSMutableArray*callBackNames = [NSMutableArrayarray];

inti =0;

for(UIImage*imageinimages) {

if(image) {

NSBlockOperation*operation = [NSBlockOperationblockOperationWithBlock:^{

//任务执行

OSSPutObjectRequest* put = [OSSPutObjectRequestnew];

put.bucketName = BucketName;

NSString*imageName = [kTempFolder stringByAppendingPathComponent:[[NSUUID UUID].UUIDString stringByAppendingString:@".jpg"]];

put.objectKey= imageName;

[callBackNamesaddObject:imageName];

NSData*data =UIImageJPEGRepresentation(image,0.3);

put.uploadingData= data;

OSSTask* putTask = [client putObject:put];

[putTaskwaitUntilFinished];//阻塞直到上传完成

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) {

[operationaddDependency:queue.operations.lastObject];

}

[queueaddOperation:operation];

}

i++;

}

if(!isAsync) {

[queuewaitUntilAllOperationsAreFinished];

NSLog(@"haha");

if(complete) {

if(complete) {

complete([NSArray arrayWithArray:callBackNames], UploadImageSuccess);

}

}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容