1

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

    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。有没有什么方法可以让我拍一张照片并将其上传到服务器并获取该 url 并将其附加到参数中或找到任何替代方法。并且正在使用 Unirest http库发送请求。

4

1 回答 1

2

为了使用 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];
于 2013-10-15T12:53:02.543 回答