gpt4 book ai didi

ios - Imagga.com 图像识别/内容 - 图像上传失败,请求超时

转载 作者:行者123 更新时间:2023-11-29 12:25:41 25 4
gpt4 key购买 nike

我尝试上传图片http://api.imagga.com/v1/content但是失败了,这里是代码

AFHTTPRequestOperationManager *taggingManager = [AFHTTPRequestOperationManager manager];

[taggingManager setRequestSerializer:[AFHTTPRequestSerializer serializer]];

[taggingManager.requestSerializer setAuthorizationHeaderFieldWithUsername: @"#" password: @"#"];

NSString *imagePath = [[NSBundle mainBundle]pathForResource:@"apple" ofType:@"jpeg"];

NSData *imageData = [NSData dataWithContentsOfFile:imagePath];

[taggingManager POST:@"http://api.imagga.com/v1/content"

parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

[formData appendPartWithFormData:imageData name:@"image"]; } success:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@"Success: %@", responseObject);} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"Error: %@", error);}];

出现此错误:Error Domain=NSURLErrorDomain Code=-1001 “请求超时。”

最佳答案

我得到了 imagga 团队的帮助,效果很好。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
[manager.requestSerializer setTimeoutInterval:10]; // 10 sec. timeout for the content upload itself
[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:api_key password:api_secret];
[manager POST:@"http://api.imagga.com/v1/content" parameters:nil constructingBodyWithBlock:^(id formData) {
[formData appendPartWithFileData:imageData
name:@"file"
fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", responseObject);

NSString *content_id = [[[responseObject valueForKey:@"uploaded"] objectAtIndex:0] valueForKey:@"id"];
NSLog(@"CONTENT ID: %@", content_id);


AFHTTPRequestOperationManager *taggingManager = [AFHTTPRequestOperationManager manager];
[taggingManager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
[taggingManager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
[taggingManager.requestSerializer setTimeoutInterval:10]; // 10 sec. timeout for the tagging itself
[taggingManager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
[taggingManager.requestSerializer setAuthorizationHeaderFieldWithUsername:api_key password:api_secret];

NSLog(@"TAGGING Request for content ID: %@", content_id);

[taggingManager GET:@"http://api.imagga.com/v1/tagging" parameters:@{@"content":content_id}
success:^(AFHTTPRequestOperation *taggingOperation, id taggingResponseObject)
{
NSLog(@"TAGGING Response: %@", taggingResponseObject);

NSArray * tags = [[[taggingResponseObject valueForKey:@"results"] objectAtIndex:0] valueForKey:@"tags"];
for (id entry in tags) {
NSString *tag = [entry valueForKey:@"tag"];
float confidence = [[entry valueForKey:@"confidence"] floatValue];

// ... do something with each tag and its confidence score
}
} failure:^(AFHTTPRequestOperation *taggingOperation, NSError *taggingError) {
NSLog(@"Tagging Error: %@", taggingError);
}];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"Upload Error: %@", error);
}];

关于ios - Imagga.com 图像识别/内容 - 图像上传失败,请求超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29229272/

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