iOS - 使用ImageShack JSON API上传图片

6

我正在尝试使用ImageShack的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;
}

有人能帮我吗?怎样才是正确的方法呢?


你尝试过发送 NSData 而不是 NSString 吗? - Sharon Nathaniel
是的,我尝试了并且收到了相同的错误响应。 - Oleg
你尝试使用非常小的图像了吗?以确保这不是超时问题。 - user207616
是的,我正在上传小图像 54x54 像素。 - Oleg
3个回答

3

好的,我已经解决了我的问题。所有参数都必须在表单体中设置,不能作为请求值。 看起来非常简单:

 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
尝试这个并告诉我们是否有效:
      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];
    }

这是一个可能会对您有帮助的问题链接:http://stackoverflow.com/questions/931088/http-post-to-imageshack - Sharon Nathaniel

1
上传图片(基础版)
 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.

你是否使用了密钥请求并获取了自己的密钥以进行上传过程?

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

同时设置格式为 application/json。请保留 HTML 标记。

是的,我使用了密钥。它显示了一个错误,告诉我当我没有使用它时应该这样做,所以我确定问题与API密钥无关。 - Oleg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接