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

时间:2015-03-24 09:42:07

标签: ios objective-c image-recognition

我尝试上传图片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);}];

收到此错误:错误域= NSURLErrorDomain代码= -1001“请求超时。”

1 个答案:

答案 0 :(得分:1)

我得到了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);
}];