0
我試圖上傳一個圖像到IMAGGA API並找回我的項目的一些圖像識別標籤,但我無法獲得所需的標籤。我用Alamofire上傳圖片,但我得到這個錯誤,並沒有來自API調用的標籤。使用Imagga API和Alamofire
這是我用來上傳圖片的功能。 代碼:
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
}
}
}
我得到這個錯誤。它最終顯示了成功,但我不確定圖像是否被髮送到圖像識別的圖像。
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
切勿在公共論壇上張貼api密鑰。 – nathan