J'essayais de télécharger une image dans l'API IMAGGA et de récupérer des tags de reconnaissance d'image pour mon projet, mais je n'arrive pas à obtenir les tags souhaités. J'ai utilisé Alamofire pour télécharger l'image, mais je reçois cette erreur et aucun tag de l'appel de l'API.Utilisation d'Imagga API et d'Alamofire
Voici la fonction que j'ai utilisée pour télécharger l'image. code:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
defer {
picker.dismiss(animated: true)
}
print(info)
// get the image
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
return
}
imageView.image = image
let documentDirectory: NSString = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as NSString
let imageName = "temp"
let imagePath = documentDirectory.appendingPathComponent(imageName)
if let data = UIImageJPEGRepresentation(image, 80) {
// Save cloned image into document directory
let urlFile = NSURL(string: imagePath)
do {
try data.write(to: urlFile! as URL, options: .atomic)
} catch {
print(error)
}
// Save it's path
localPath = imagePath
}
let imageData = UIImagePNGRepresentation(image)!
//
let headers: HTTPHeaders = [
"Authorization": "Basic YWNjX2MwNDUzYzkzNTEyOGNkYzo0ZmE5MWM4Zjg0MDk1ZGI0NGE2ZjNjODJkNTczZDUxOQ==",
"Accept": "application/json"
]
Alamofire.request("https://httpbin.org/basic-auth/\("acc_c0453c935128cdc")/\("4fa91c8f84095db44a6f3c82d573d519")", parameters: ["url": "http://docs.imagga.com/static/images/docs/sample/japan-605234_1280.jpg"], headers: headers)
.authenticate(user: "acc_c0453c935128cdc", password: "4fa91c8f84095db44a6f3c82d573d519")
.responseJSON { response in
debugPrint(response)
switch(response.result) {
case .success(_):
if let data = response.result.value{
print("YOUR JSON DATA>> \(response.data!)")
}
break
case .failure(_):
print(response.result.error)
break
}
}
}
Je reçois ce que l'erreur. Il montre du succès à la fin mais je ne suis pas sûr si l'image est envoyée à l'imagga pour la reconnaissance d'image ou pas.
Error:
URL which has no scheme
Error Domain=NSCocoaErrorDomain Code=518 "The file couldn’t be saved because the specified URL type isn’t supported." UserInfo={NSURL=/var/mobile/Containers/Data/Application/D8A0EE1D-2092-474F-B21A-8A7E0635AD40/Documents/temp}
[Request]: GET https://httpbin.org/basic-auth/acc_c0453c935128cdc/4fa91c8f84095db44a6f3c82d573d519?url=http%3A//docs.imagga.com/static/images/docs/sample/japan-605234_1280.jpg
[Response]: <NSHTTPURLResponse: 0x1702210e0> { URL: https://httpbin.org/basic-auth/acc_c0453c935128cdc/4fa91c8f84095db44a6f3c82d573d519?url=http%3A//docs.imagga.com/static/images/docs/sample/japan-605234_1280.jpg } { status code: 200, headers {
"Access-Control-Allow-Credentials" = true;
"Access-Control-Allow-Origin" = "*";
Connection = "keep-alive";
"Content-Length" = 62;
"Content-Type" = "application/json";
Date = "Tue, 29 Aug 2017 16:42:07 GMT";
Server = "meinheld/0.6.1";
Via = "1.1 vegur";
"X-Powered-By" = Flask;
"X-Processed-Time" = "0.000633001327515";
} }
[Data]: 62 bytes
[Result]: SUCCESS: {
authenticated = 1;
user = "acc_c0453c935128cdc";
}
[Timeline]: Timeline: { "Request Start Time": 525717726.320, "Initial Response Time": 525717728.876, "Request Completed Time": 525717728.882, "Serialization Completed Time": 525717728.889, "Latency": 2.556 secs, "Request Duration": 2.562 secs, "Serialization Duration": 0.006 secs, "Total Duration": 2.569 secs }
YOUR JSON DATA>> 62 bytes
Ne publiez jamais de clés api sur des forums publics. – nathan