iOS - 使用 ImageShack JSON API 上传图片
Posted
技术标签:
【中文标题】iOS - 使用 ImageShack JSON API 上传图片【英文标题】:iOS - upload picture with ImageShack JSON API 【发布时间】:2013-05-30 08:45:37 【问题描述】:我正在尝试使用他们的API 将图片上传到 ImageShack:
- (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
?
是的,我做了并且得到了相同的错误响应
您尝试过使用非常小的图像吗? /以确保它不是超时问题
是的,我正在上传 54x54 像素的小图片
【参考方案1】:
好的,我已经设法解决了我的问题。所有参数都必须在表单正文中设置,而不是作为请求值。 看起来很简单:
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];
【讨论】:
这里是一个可以帮助你的问题的链接:***.com/questions/931088/http-post-to-imageshack【参考方案3】:上传图片(基本)
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
【讨论】:
是的,我使用密钥。它向我显示了一个错误,我应该在我不使用它时执行它,所以我确定问题与 API 密钥无关以上是关于iOS - 使用 ImageShack JSON API 上传图片的主要内容,如果未能解决你的问题,请参考以下文章
使用 cURL 将 PHP 文件上传到 imageshack