Reputation: 87
I am trying to implement the Vize.ai image recognition in an iOS app using Swift 4.
In their documentation this is the code example they give for Objective C:
NSDictionary *headers = @{@"Authorization": @"JWT {your JWT token}", @"Content-Type": @"application/x-www-form-urlencoded", @"Accept": @"text/plain"};
UNIUrlConnection *asyncConnection = [[UNIRest post:^(UNISimpleRequest *request) {
[request setUrl:@"http://cl-api.vize.ai/{your task ID}?image={path/myimage.png}"];
[request setHeaders:headers];
}] asundefinedAsync:^(UNIHTTPundefinedResponse *response, NSError *error) {
NSInteger code = response.code;
NSDictionary *responseHeaders = response.headers;
UNIJsonNode *body = response.body;
NSData *rawBody = response.rawBody;
}];
As you can see I have to pass an image path to the request. In my app the user can either chose to analyse a default picture that is added to the project's assets folder or add from library/take a photo.
What is that image Path supposed to be in this example?
Here is how I am making the request with Swift 4, any image path I am adding to it it gives me an "missing image or url" response error back:
let headers: HTTPHeaders = [
"Authorization": "JWT \(jwtToken)",
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "text/plain"
]
let url = "https://cl-api.vize.ai/\(taskID)?image=\(imagePath)"
Alamofire.request(url, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
debugPrint(response)
}
Any help is greatly appreciated. Thanks!
Upvotes: 3
Views: 255
Reputation: 87
So I managed to find a solution for this using multipart form data. Here is the complete code for it.
func getVizeImageAnalysis(image: UIImage) {
let headers: HTTPHeaders = [
"Authorization": "JWT \(jwtToken)",
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "text/plain"
]
let url = "https://cl-api.vize.ai/\(taskID)"
manager.upload(multipartFormData: { multiPartData in
// Add image
if let imageData = UIImageJPEGRepresentation(image, 0.8) {
multiPartData.append(imageData, withName: "image", fileName: "pickedImage", mimeType: "image/jpeg")
}
}, to: url, method: .post, headers: headers, encodingCompletion: {
encodingResult in
switch encodingResult {
case .success(let request, _, _):
request.responseJSON{ response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
})
}
Upvotes: 2