我在这里阅读alchemy-API文档:
http://www.alchemyapi.com/api/image-tagging/image.html
他们说图片必须经过URI编码...这到底是什么意思?
这是否意味着将图像转换为base64字符串,然后将其传递给请求?
我已经尝试过,但是收到了HTTP 414错误-请求URI太大。
这是我发出请求的代码:
@IBAction func analyzeImage(sender: UIButton) {
var imageData = UIImagePNGRepresentation(mainImage.image)
let base64ImageString = imageData.base64EncodedStringWithOptions(.allZeros)
let requestString = ENDPOINT+"?apikey="+API_KEY+"&image="+base64ImageString+"&outputMode=json"
let url = NSURL(string: requestString)
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}
task.resume()
}
编辑:我考虑了Dijkgraaf的建议,即使用POST请求而不是GET来解决URI长度问题。我正在使用Alamofire库来执行此操作。这是我的代码:
@IBAction func analyzeImage(sender: UIButton) {
var imageData = UIImagePNGRepresentation(mainImage.image)
let base64ImageString = imageData.base64EncodedStringWithOptions(.allZeros)
let params = [
"apikey" : API_KEY,
"image" : base64ImageString,
"outputMode" : "json"]
var manager = Manager.sharedInstance
//Passing all the headers you want!
manager.session.configuration.HTTPAdditionalHeaders = [
"Content-Type": "application/x-www-form-urlencoded"
]
manager.request(.POST, ENDPOINT, parameters:params, encoding: .URL)
.response { (request, response, data, error) in
println(request)
println(response)
println(error)
}
}
但是,尝试此操作时出现“无法分析:下游问题”错误。
这是控制台输出:
<NSMutableURLRequest: 0x1742040c0> { URL: http://access.alchemyapi.com/calls/image/ImageGetRankedImageKeywords }
Optional(<NSHTTPURLResponse: 0x17082c1e0> { URL: http://access.alchemyapi.com/calls/image/ImageGetRankedImageKeywords } { status code: 200, headers {
"Access-Control-Allow-Origin" = "*";
"Cache-Control" = "no-cache";
Connection = "keep-alive";
"Content-Length" = 326;
"Content-Type" = "application/json";
Date = "Mon, 08 Jun 2015 05:59:22 GMT";
Server = nginx;
"X-AlchemyAPI-CurrentVersion" = "12.15";
"X-AlchemyAPI-Error-Msg" = "cannot-analyze:downstream-issue";
"X-AlchemyAPI-Key" = [API KEY HIDDEN];
"X-AlchemyAPI-Params" = "sentiment=0&knowledgeGraph=0&detectedLanguage=unknown&submitLanguage=detect";
"X-AlchemyAPI-Status" = ERROR;
"X-AlchemyAPI-Total-Transactions" = 0;
} })
nil
不知道出了什么问题,但是Alchemy文档确实声明POST请求应将“Content-Type” header 设置为“application/x-www-form-urlencoded”,无论如何都不会发生我尝试设置。这可能是问题吗?
编辑:我尝试过仅发布原始图像数据,正如Dijkgraaf建议的那样:
@IBAction func analyzeImage(sender: UIButton) {
var imageData = UIImagePNGRepresentation(mainImage.image)
//let base64ImageString = imageData.base64EncodedStringWithOptions(.allZeros)
var request = HTTPTask()
request.requestSerializer = HTTPRequestSerializer()
request.requestSerializer.headers["Content-Type"] = "application/x-www-form-urlencoded"
let params: Dictionary<String,AnyObject> = [
"apikey" : API_KEY,
"imagePostMode" : "raw",
"image" : imageData,
"outputMode" : "json"]
request.POST(ENDPOINT, parameters: params, completionHandler: {(response: HTTPResponse) in
println(response.headers)
})
}
但是我仍然遇到相同的无法分析下游问题的错误。
最佳答案
使用imagePostMode raw时,需要将图像数据作为POST请求的主体发送,并且参数应包含在端点URL中(例如ENDPOINT =“http://access.alchemyapi.com/calls/image/ImageGetRankedImageKeywords?apikey = API_KEY&outputMode = json&imagePostMode = raw“)。我没有与Swift合作,所以我不知道执行此操作的最佳方法,但这与您可能期望的有所不同。
关于swift - 如何对图像进行URI编码?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30700941/