2017-01-23 8 views
2

HTTPS POSTリクエストを使用してCloudnhostingに画像をアップロードできません。SDKの代わりに単純なAPIメソッドを使用したいと思います。私はバイト配列バッファーまたはBase64エンコードされたイメージをフォーマットする際に問題があります。画像をCloudly 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を...」

「私は本当に他の方法の多くを試してみましたが、私はすることができますそれを働かせてください。

更新:私はURLリンクを 'file' paramに置くとすべて正常に動作します。

答えて

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

私はCloudine APIに関する経験はありませんが、これが役立つことを願っています。私は 'Uploading with a direct call to the API'のドキュメントを見ました。

あなたはPOSTパラメータ(本体のデータとして連結されていない)として 'file'と 'upload_preset'を指定する必要があると思います。

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

ここで説明したようにあなたがアップロード画像のタスクを実行するために、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:@{}]; 

次のコードサンプルは、カスタムパブリックID user_sample_image_1002を指定し、後でアップロードされたイメージにアクセスし、タグを割り当ててイメージの管理を簡単にする例を示しています。さらに、アップロードされた画像の150×100の顔検出ベースのサムネイルをアプリケーションに埋め込むために、画像処理を実行する動的URLを作成する例を示します。

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); 
}]; 
関連する問題