2013-05-30 10 views
6

で画像をアップロードする:ノーエラーの説明とバックのエラーメッセージが表示されます応答としてiOSの - 私は彼らの<a href="https://code.google.com/p/imageshackapi/wiki/ImageshackAPI" rel="noreferrer">API</a>を使用してImageShackのに写真をアップロードしようとしているImageShackのJSON APIを

- (void)uploadImage2:(UIImage *)image 
{ 
    NSData *imageToUpload = UIImagePNGRepresentation(image); 

    if (imageToUpload) 
    { 
     NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; 
     [parameters setObject:@"XXXX" forKey:@"key"]; 
     [parameters setObject:@"json" forKey:@"format"]; 
     //[parameters setObject:@"application/json" forKey:@"Content-Type"]; 

     AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"https://post.imageshack.us"]]; 

     NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/upload_api.php" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
      [formData appendPartWithFileData: imageToUpload name:@"image" fileName:@"logo.png" mimeType:@"image/png"]; 
     }]; 

     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

     [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
     { 
      NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil]; 
      NSLog(@"response: %@",jsons); 

     } 
             failure:^(AFHTTPRequestOperation *operation, NSError *error) 
     { 
      if([operation.response statusCode] == 403) 
      { 
       //NSLog(@"Upload Failed"); 
       return; 
      } 
      //NSLog(@"error: %@", [operation error]); 

     }]; 

     [operation start]; 
    } 
} 

{ 
    "error_code" = "upload_failed"; 
    "error_message" = "Upload failed"; 
    status = 0; 
} 

誰も助けてくれますか?それを行う正しい方法は何ですか?

+0

あなたは 'NSString'の代わりに' NSData'を送信しようとしましたか? –

+0

はい、私はして、同じエラー応答 – Oleg

+0

を得て、あなたは非常に小さな画像で試してみましたか? /タイムアウトの問題ではないことを確認する –

答えて

3

設定

Obtaining an API Key 
To obtain an API key, please use our API Key Request form. 

さて、私は私の問題を解決するために管理しています。すべてのパラメータは、要求値としてではなく、フォーム本体で設定する必要があります。 それは非常に簡単になります。これは、誰かを助ける

NSData *imageToUpload = UIImagePNGRepresentation([UIImage imageNamed:@"logo.png"]); 


    if (imageToUpload) 
    { 
     NSString *urlString = @"https://post.imageshack.us"; 

     AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:urlString]]; 
     NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload_api.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
      [formData appendPartWithFileData:imageToUpload name:@"fileupload" fileName:@"image" mimeType:@"image/png"]; 
      [formData appendPartWithFormData:[@"XXXXXX" dataUsingEncoding:NSUTF8StringEncoding] name:@"key"]; 
      [formData appendPartWithFormData:[@"json" dataUsingEncoding:NSUTF8StringEncoding] name:@"format"]; 
     }]; 



     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

     [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
     { 
      NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil]; 
      NSLog(@"response: %@",jsons); 

     } 
             failure:^(AFHTTPRequestOperation *operation, NSError *error) 
     { 
      if([operation.response statusCode] == 403) 
      { 
       NSLog(@"Upload Failed"); 
       return; 
      } 
      NSLog(@"error: %@", [operation error]); 

     }]; 

     [httpClient enqueueHTTPRequestOperation:operation]; 
    }} 

願っています!

2

これを試してみて、それが動作するかどうかはお知らせ:画像(基本)

Endpoint : https://post.imageshack.us/upload_api.php Parameters : 
* key : The api key for your application; found in email sent after filling out our API Key Request form 
* fileupload : image file 
* format : json tags : a CSV list of tags to describe your picture public : A string setting your image to public or private where "yes" is public and "no" is private. Images are public by default. 

をアップロード

 NSData *imageToUpload = UIImageJPEGRepresentation(uploadedImgView.image,1.0);//(uploadedImgView.image); 
     if (imageToUpload) 
     { 
     NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; 
     [parameters setObject:@"MY API KEY" forKey:@"key"]; 
     [parameters setObject:@"json" forKey:@"format"]; 

     AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"https://post.imageshack.us"]]; 

     NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/upload_api.php" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
      [formData appendPartWithFileData: imageToUpload name:@"image" fileName:@"temp.jpeg" mimeType:@"image/jpeg"]; 
     }]; 

     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

     [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
     { 
      NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil]; 
      //NSLog(@"response: %@",jsons); 

     } 
             failure:^(AFHTTPRequestOperation *operation, NSError *error) 
     { 
      if([operation.response statusCode] == 403) 
      { 
       //NSLog(@"Upload Failed"); 
       return; 
      } 
      //NSLog(@"error: %@", [operation error]); 

     }]; 

     [operation start]; 
    } 
+0

エラーのある同じJSON応答 – Oleg

+0

はここにあなたを助けることができる質問のリンクです:http://stackoverflow.com/questions/ 931088/http-post-to-imageshack –

1

あなたがキー要求を使用してアップロードプロセスのための独自のキーを取得しましたか?また、フォーマットapplication/json

+0

はい、キーを使用します。それは私がそれを使用していないときに私はそれを行う必要があるエラーを示したので、問題はAPIキーに関連していないと確信しています – Oleg

+0

あなたは答えとテストで指定された形式を設定しますか? –

+0

はい、私は、幸運:-( – Oleg

関連する問題