将图像作为二进制文件发送到服务器
以下是我发送图像到服务器以获取图像信息的属性。
NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"en_US", @"image_request[locale]", @"en", @"image_request[language]",[NSURL URLWithString:@"<file url>"], @"image_request[image]", nil];
要使用以下代码上传图片:
NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
但参数是要求我提供的url.Is有任何方式,只要我拍一张照片,并上传到服务器,并获得该url,并附加在参数或任何其他替代被发现。而且我使用Unirest http库发送请求。
为了使用Unirest将摄像头捕获的图像传输到CamFind API,您需要先使图像相同。
// Get the path to the Documents folder NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirectoryPath = [paths objectAtIndex:0]; // Get the path to an file named "tmp_image.jpg" in the Documents folder NSString *imagePath = [documentDirectoryPath stringByAppendingPathComponent:@"tmp_image.jpg"]; NSURL *imageURL = [NSURL fileURLWithPath:imagePath]; // Write the image to an file called "tmp_image.jpg" in the Documents folder NSData *imageData = UIImageJPEGRepresentation(image, 1.0); [imageData writeToURL:imageURL atomically:YES]; // Now construct the parameters that will be passed to Unirest NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"en_US", @"image_request[locale]", @"en", @"image_request[language]", imageURL, @"image_request[image]", nil]; // And the headers NSDictionary* headers = [NSDictionary dictionaryWithObjectsAndKeys:@"<mashape-key>", @"X-Mashape-Authorization", nil]; // Call the API using Unirest HttpJsonResponse* response = [[Unirest post:^(BodyRequest* request) { [request setUrl:@"https://camfind.p.mashape.com/image_requests"]; [request setHeaders:headers]; [request setParameters:parameters]; }] asJson];