Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将图像作为二进制文件发送到服务器_Ios_Objective C - Fatal编程技术网

Ios 将图像作为二进制文件发送到服务器

Ios 将图像作为二进制文件发送到服务器,ios,objective-c,Ios,Objective C,下面是我将图像发送到服务器以获取图像信息的属性 NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"en_US", @"image_request[locale]", @"en", @"image_request[language]",[NSURL URLWithString:@"<file url>"], @"image_request[image]", nil]; 但参数要求我提

下面是我将图像发送到服务器以获取图像信息的属性

    NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"en_US", @"image_request[locale]", @"en", @"image_request[language]",[NSURL URLWithString:@"<file url>"], @"image_request[image]", nil];

但参数要求我提供url。是否有任何方法,只要我抓拍一张照片并将其上传到服务器,并获取该url并将其附加到参数或任何可找到的备选方案中。我使用Unirest http库发送请求。

以便使用Unirest,您需要先对图像进行相同处理

// 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];
//获取文档文件夹的路径
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentDirectoryPath=[paths objectAtIndex:0];
//获取文档文件夹中名为“tmp_image.jpg”的文件的路径
NSString*imagePath=[documentDirectoryPath stringByAppendingPathComponent:@“tmp_image.jpg]”;
NSURL*imageURL=[NSURL fileURLWithPath:imagePath];
//将图像写入Documents文件夹中名为“tmp_image.jpg”的文件
NSData*imageData=UIImageJPEG表示法(图像,1.0);
[imageData writeToURL:imageURL原子性:是];
//现在构造将传递给Unirest的参数
NSDictionary*参数=[NSDictionary Dictionary WithObjectsSandKeys:@“en_-US”,“image_请求[locale],@“en”,“image_请求[language]”,imageURL,@“image_请求[image]”,nil];
//标题呢
NSDictionary*headers=[NSDictionary Dictionary WithObjectsSandKeys:@“”@“X-Mashape-Authorization”,无];
//使用Unirest调用API
HttpJsonResponse*response=[[Unirest post:^(BodyRequest*request){
[请求设置URL:@”https://camfind.p.mashape.com/image_requests"];
[请求集标题:标题];
[请求设置参数:参数];
}]asJson];

您的问题不是很清楚。你说服务器要求你上传一个二进制图像,然后你说服务器需要一个图像URL。这是哪一个?如果您转到此属性,则该属性需要NSURL,但端点描述显示为二进制。我将尝试此操作。但我尝试使用BodyRequest,而不是HttpJsonResponse响应=[[Unirest postEntity:^(MultipartRequest request)];区别是什么?从哪里可以获得更多Unirest库的示例?如何使用UnirestJsonNode*json=[response body]打印json输出;NSLog(@“Answer=%@”,json);输出-答案=。。如何使用大括号获取json输出..这应该是一个单独的问题,而不是注释。
// 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];