美文网首页
阿里云上传图片

阿里云上传图片

作者: 文扉Song | 来源:发表于2017-03-14 21:53 被阅读148次

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

    }

    }

    }

    }

    相关文章

      网友评论

          本文标题:阿里云上传图片

          本文链接:https://www.haomeiwen.com/subject/iehlbttx.html