gpt4 book ai didi

swift - 如何对图像进行 URI 编码?

转载 作者:IT王子 更新时间:2023-10-29 05:50:39 26 4
gpt4 key购买 nike

我正在阅读这里的 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)
}
}

但是,当我尝试此操作时,出现“cannot-analyze:downstream-issue”错误。

这是控制台输出:

<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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com