我有一个AFHTTPRequestOperationManager
POST
用于检索 json 面部特征的方法:
+(Face *)getFace:(UIImage *) image
{
__block Face *face = [[Face alloc]init];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"selector": @"FULL"};
NSData *imageData = UIImagePNGRepresentation(image);
[manager POST:@"http://api.animetrics.com/v1/detect" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData: imageData name:@"image" fileName:[NSString stringWithFormat:@"file%d.png",1] mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError* error;
face = [[Face alloc] initWithString:responseObject error:&error];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
return face;
}
我使用 GCD 在我的视图控制器中调用了这个方法:
dispatch_queue_t queue = dispatch_queue_create("My Queue",NULL);
dispatch_async(queue, ^{
Face *face = [Face getFace:[UIImage imageNamed:@"image.png"]];
dispatch_async(dispatch_get_main_queue(), ^{
[face getPoints];
});
});
问题[face getPoints]
总是返回null
,因为它在getFace
方法完成检索 json 对象之前被执行。我认为那是因为 AFNetworking 本身正在使用 GCD。但是我该如何解决呢?我错过了什么?我正在使用最新的 AFNetworking 2.0。