gpt4 book ai didi

iphone - iOS - 使用 ImageShack JSON API 上传图片

转载 作者:可可西里 更新时间:2023-11-01 03:52:18 25 4
gpt4 key购买 nike

我正在尝试使用他们的 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 *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];
}}

希望这会对某人有所帮助!

关于iphone - iOS - 使用 ImageShack JSON API 上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16831628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com