我正在使用 camfind api,我发布了一个请求,向 api 发送图像并获取令牌,然后我可以使用令牌来获取图像是什么,但这需要两个步骤,一个发布和一个获取请求. 我想将它们组合起来,因此当服务器响应发布请求时,get 请求会自动触发。这是代码:
我在顶部为令牌指定了一个值
NSString *tokenValue;
请求方法,这里选择一个图像并将其发送到服务器,服务器在此请求中发回一个令牌响应
- (void *) requestMethod: (UIImage *)imageToConvert{
NSString *temp = @"saved";
NSLog(@"%@", temp);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectoryPath = [paths objectAtIndex:0];
NSString *imagePath = [documentDirectoryPath stringByAppendingPathComponent:@"tmp_image.jpg"];
NSURL *imageURL = [NSURL fileURLWithPath:imagePath];
NSData *imageData = UIImageJPEGRepresentation(imageToConvert , 1.0);
[imageData writeToURL:imageURL atomically:YES];
// NSString *base64image = [NSString stringWithFormat:@"%@",[imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];
NSString *base64image = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
// These code snippets use an open-source library.
// NSString *baseboy = [base64image stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// NSURL *urlimage_request = [NSURL URLWithString:[base64image stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSDictionary *headers = @{@"X-Mashape-Key": @"********"};
NSDictionary *parameters = @{@"focus[x]": @"480", @"focus[y]": @"640", @"image_request[altitude]": @"27.912109375", @"image_request[image]": imageURL, @"image_request[language]": @"en", @"image_request[latitude]": @"35.8714220766008", @"image_request[locale]": @"en_US", @"image_request[longitude]": @"14.3583203002251"};
UNIUrlConnection *asyncConnection = [[UNIRest post:^(UNISimpleRequest *request) {
[request setUrl:@"https://camfind.p.mashape.com/image_requests"];
[request setHeaders:headers];
[request setParameters:parameters];
}] asJsonAsync:^(UNIHTTPJsonResponse *response, NSError *error) {
NSInteger code = response.code;
NSDictionary *responseHeaders = response.headers;
UNIJsonNode *body = response.body;
NSData *rawBody = response.rawBody;
NSString *token = response.description;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response.rawBody
options:kNilOptions
error:nil];
NSLog(@"Response status: %ld\n%@", (long) response.code, json);
for(NSString *key in [json allValues])
{
tokenValue = [json valueForKey: @"token" ];
NSString *two = [json valueForKey: @"token" ]; // assuming the value is indeed a string
NSLog(@"Token :%@", two);
NSString *one = @"https://camfind.p.mashape.com/image_responses" ;
NSDictionary *headers = @{@"X-Mashape-Key": @"9hcyYCUJEsmsh4lNTgpgVX1xRq0Ip1uogovjsn5Mte0ONVBtes", @"Accept": @"application/json"};
NSString *responseString = [NSString stringWithFormat:@"%@/%@", one, two];
// NSString *responseURL = [one stringByAppendingString:two];
NSLog(@"response URL %@", responseString);
// UNIUrlConnection *asyncConnection = [[UNIRest get:^(UNISimpleRequest *request) {
// [request setUrl:responseString];
// [request setHeaders:headers];
// }] asJsonAsync:^(UNIHTTPJsonResponse *response, NSError *error) {
// NSInteger code = response.code;
// NSDictionary *responseHeaders = response.headers;
// UNIJsonNode *body = response.body;
// NSData *rawBody = response.rawBody;
// NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response.rawBody
// options:kNilOptions
// error:nil];
// NSLog(@"Response status: %ld\n%@", (long) response.code, json);
//
//
// // NSLog(@"didfinishLoadingbody%@",rawBody);
// // NSLog(@"didfinishLoadingbody%@",body);
// //
// // NSLog(@"didfinishLoading responseheader%@",responseHeaders);
// // NSLog(@"didfinishLoading tok%@",token);
// }];
}
// NSLog(@"didfinishLoadingbody%@",rawBody);
// NSLog(@"didfinishLoadingbody%@",body);
//
// NSLog(@"didfinishLoading responseheader%@",responseHeaders);
// NSLog(@"didfinishLoading tok%@",token);
}];
return (__bridge void *)(self);
}
下面的响应方法使用上面请求中的令牌打印出图像是什么
- (void *) responseMethod {
// These code snippets use an open-source library.
NSString *one = @"https://camfind.p.mashape.com/image_responses" ;
NSString *responseString = [NSString stringWithFormat:@"%@/%@", one, tokenValue];
NSDictionary *headers = @{@"X-Mashape-Key": @"9hcyYCUJEsmsh4lNTgpgVX1xRq0Ip1uogovjsn5Mte0ONVBtes", @"Accept": @"application/json"};
UNIUrlConnection *asyncConnection = [[UNIRest get:^(UNISimpleRequest *request) {
[request setUrl:responseString];
[request setHeaders:headers];
}] asJsonAsync:^(UNIHTTPJsonResponse *response, NSError *error) {
NSInteger code = response.code;
NSDictionary *responseHeaders = response.headers;
UNIJsonNode *body = response.body;
NSData *rawBody = response.rawBody;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response.rawBody
options:kNilOptions
error:nil];
NSLog(@"Response status: %ld\n%@", (long) response.code, json);
// NSLog(@"didfinishLoadingbody%@",rawBody);
// NSLog(@"didfinishLoadingbody%@",body);
//
NSLog(@"didfinishLoading responseheader%@",responseHeaders);
// NSLog(@"didfinishLoading tok%@",token);
}];
return (__bridge void *)(self);
}
其余的 vc 其他东西
-(void)loadView {
[super loadView];
}
- (void)viewDidLoad{
[super viewDidLoad];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}
-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
}
我不知道如何在服务器回复时立即触发响应方法,我不知道 objc,我在 swift 项目中使用它
@end