美文网首页
[iOS]通过AFNetworking上传图片到阿里云OSS

[iOS]通过AFNetworking上传图片到阿里云OSS

作者: 雨影 | 来源:发表于2019-01-21 17:26 被阅读60次

    URL链接需要的key和token需要后台事先给你们拼好返回

    + (__kindof NSURLSessionTask *)uploadImagesToOSSWithURL:(NSString *)URL
                                                     image:(UIImage *)image
                                                   progress:(PPHttpProgress)progress
                                                    success:(PPHttpRequestSuccess)success
                                                    failure:(PPHttpRequestFailed)failure {
    
        NSData *imageData = UIImageJPEGRepresentation(image, 1);
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URL]];
        request.HTTPMethod = @"PUT";
        [request setValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
    
        NSURLSessionTask *uploadTask = [_sessionManager uploadTaskWithRequest:request
            fromData:imageData
            progress:^(NSProgress *_Nonnull uploadProgress) {
              //上传进度
              dispatch_sync(dispatch_get_main_queue(), ^{
                progress ? progress(uploadProgress) : nil;
              });
            }
            completionHandler:^(NSURLResponse *_Nonnull response, id _Nullable responseObject, NSError *_Nullable error) {
    
              if (!error) {
                  success(nil, nil);
              } else {
                  [self handelFailuerWith:nil error:error failure:failure];
              }
    
            }];
        [uploadTask resume];
    
        return uploadTask;
    }
    
    
    
    

    相关文章

      网友评论

          本文标题:[iOS]通过AFNetworking上传图片到阿里云OSS

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