2017-01-23 3 views
2

Я не могу загружать изображения в облачный хостинг через HTTPS POST-запрос, я хочу использовать простые методы API вместо SDK. У меня возникла проблема с форматированием изображения в буфере массива байтов или в кодировке Base64.Загрузить изображение в Cloudinary IOS

Вот мой код:

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 ...»

Я действительно пытался много других методов, но я могу» t заставить его работать.

UPDATE: Когда я установил ссылку URL в параметре «файл», все работает нормально.

ответ

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]; 
0

У меня нет опыта работы с API-интерфейсом CloudMan, но я надеюсь, что это будет полезно. Я посмотрел документы на «Uploading with a direct call to the API».

Я думаю, что вам необходимо предоставить «файл» и «upload_preset» в качестве параметров POST (не конкатенированных как данные в теле).

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"; 

// Replace this: 

//NSString *strRequest = [NSString stringWithFormat:@"file=%@&upload_preset=MYSECTER", strImageData]; 
//request.HTTPBody = [strRequest dataUsingEncoding:NSUTF8StringEncoding]; 

// With this: 

NSString *imageDataStr = [[NSString alloc] initWithData:imageData encoding:NSUTF8StringEncoding]; 
[request setValue:imageDataStr forHTTPHeaderField:@"file"]; 
[request setValue:@"MYSECTER" forHTTPHeaderField:@"upload_preset"]; 


[[[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]; 
+0

Привет, спасибо за ваше время, но он не работает. Я получил сообщение об ошибке: «Отсутствует необходимый параметр - файл»; –

0

Вы можете использовать SDK Cloudinary для того, чтобы выполнить задачу загрузки изображения, как описано здесь: http://cloudinary.com/blog/direct_upload_made_easy_from_browser_or_mobile_app_to_the_cloud

Следующая примеры кода показывают прямой вызов API без знака в Objective-C для iOS ...:

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, что позволяет впоследствии получить доступ к загруженному изображению, и присвоению метки, чтобы упростить управление образами. Кроме того, мы показываем пример построения динамического URL-адреса, который выполняет манипулирование изображениями «на лету»: генерирует миниатюру изображений размером 150 × 100 для загруженных изображений для встраивания в ваше приложение.

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); 
}]; 
Смежные вопросы