阿里云上传图片

2017-03-14  本文已影响148人  文扉Song

+ (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);

}

}

}

}

上一篇下一篇

猜你喜欢

热点阅读