私はcamfind APIを使用しています.APIに画像を送信してトークンを取得するリクエストを投稿し、トークンを使用して画像を取得できますが、これには投稿と取得リクエストの2つの手順が必要です. . それらを結合したいので、サーバーが投稿リクエストに応答すると、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 がわかりません。これを迅速なプロジェクトで使用しています。
@end