将图像上传到Cloudinary IOS

时间:2017-01-23 18:12:21

标签: ios objective-c uiimage nsdata

我无法通过HTTPS POST请求将图像上传到Cloudinary托管,我想使用简单的API方法而不是SDK。我在字节数组缓冲区或Base64编码格式化图像时遇到问题。

这是我的代码:

UIImage *image = [UIImage imageNamed:@"image.png"];
NSData *imageData = UIImagePNGRepresentation(image);
NSString *strImageData = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

NSURL *url = [NSURL URLWithString:@"https://api.cloudinary.com/v1_1/MYSECTER/image/upload"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
NSString *strRequest = [NSString stringWithFormat:@"file=%@&upload_preset=MYSECTER", strImageData];
request.HTTPBody = [strRequest dataUsingEncoding:NSUTF8StringEncoding];

[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

    NSDictionary *recievedData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

    NSLog(@"RECEIVED: %@", recievedData);

}] resume];

不幸的是,我从服务器收到以下答案:“不支持的源URL ...”

我真的尝试了很多其他方法,但我无法让它发挥作用。

更新:当我将“URL”链接放在“文件”参数时,一切正常。

3 个答案:

答案 0 :(得分:0)

我没有使用Cloudinary API的任何经验,但我希望这会有所帮助。我查看过' Uploading with a direct call to the API'

上的文档

我认为您需要提供'文件'和' upload_preset'作为POST参数(不作为正文中的数据连接)。

data.forEach((element) => {
  const sessionId = new Date().getTime();
  Blob.build(RNFetchBlob.wrap(element.path), { type : 'image/jpeg' })
  .then((blob) => {firebase.storage()
    .ref('images')
    .child(`${sessionId}`)
    .put(blob, { contentType : 'image/png' })
    .then((snapshot) => {
      element.image = snapshot.metadata.downloadURLs[0];
    })
  });
});

Parse.Cloud.run('doSomething', {
  data : data,
}).then(res => {
  dispatch({
    type: 'WE_DID_SOMETHING',
  });
});

答案 1 :(得分:0)

我找到解决方案,效果非常好:

// image for sending
UIImage *image = [UIImage imageNamed:@"image.png"];

// Dictionary that holds post parameters. You can set your post parameters that your server accepts or programmed to accept.
NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
[_params setObject:@"SECKET_PRESET" forKey:@"upload_preset"];

// the boundary string : a random string, that will not repeat in post data, to separate post data fields.
NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy";

// string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ
NSString* FileParamConstant = @"file";

// the server url to which the image (or the media) is uploaded. Use your server url here
NSURL* requestURL = [NSURL URLWithString:@"https://api.cloudinary.com/v1_1/SECKET_KEY/image/upload"];

// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];

// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

// post body
NSMutableData *body = [NSMutableData data];

// add params (all params are strings)
for (NSString *param in _params) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}

// add image data
NSData *imageData = UIImagePNGRepresentation(image);
if (imageData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the reqeust
[request setHTTPBody:body];

// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

// set URL
[request setURL:requestURL];

[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

    NSDictionary *recievedData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

    NSLog(@"RECEIVED: %@", recievedData);

}] resume];

答案 2 :(得分:0)

您可以使用Cloudinary的SDK执行上传图片任务,如下所述:http://cloudinary.com/blog/direct_upload_made_easy_from_browser_or_mobile_app_to_the_cloud

  

以下代码示例显示了Objective-C for iOS中的直接无符号上传API调用...:

CLCloudinary *cloudinary = [[CLCloudinary alloc] init];
[cloudinary.config setValue:@"demo" forKey:@"cloud_name"];

NSString *imageFilePath = [[NSBundle mainBundle] pathForResource:@"logo" 
ofType:@"png"];

CLUploader* uploader = [[CLUploader alloc] init:cloudinary delegate:self];
[uploader unsignedUpload:imageFilePath uploadPreset:@"zcudy0uz" options:@{}];
  

以下代码示例显示了一个更高级的示例:指定user_sample_image_1002的自定义公共ID,以便以后访问上载的图像,并指定标记以简化图像管理。此外,我们还展示了构建动态URL的示例,该动态URL执行即时图像处理:生成基于面部检测的150x100上传图像缩略图,以便嵌入到您的应用程序中。

NSData *imageData = [NSData dataWithContentsOfFile:imageFilePath];

[uploader unsignedUpload:imageData uploadPreset:@"zcudy0uz" options:
[NSDictionary dictionaryWithObjectsAndKeys:@"user_sample_image_1002", 
@"public_id", @"tags", @"ios_upload", nil] withCompletion:^(NSDictionary 
*successResult, NSString *errorResult, NSInteger code, id context) {

    if (successResult) {

      NSString* publicId = [successResult valueForKey:@"public_id"];
      NSLog(@"Upload success. Public ID=%@, Full result=%@", publicId, 
      successResult);
      CLTransformation *transformation = [CLTransformation transformation];
      [transformation setWidthWithInt: 150];
      [transformation setHeightWithInt: 100];
      [transformation setCrop: @"fill"];
      [transformation setGravity:@"face"];

      NSLog(@"Result: %@", [cloudinary url:publicId 
      options:@{@"transformation": 
      transformation, @"format": @"jpg"}]);                

     } else {

         NSLog(@"Upload error: %@, %d", errorResult, code);            

      }

 }   andProgress:^(NSInteger bytesWritten, NSInteger totalBytesWritten, 
     NSInteger totalBytesExpectedToWrite, id context) {
     NSLog(@"Upload progress: %d/%d (+%d)", totalBytesWritten, 
     totalBytesExpectedToWrite, bytesWritten);
 }];