gpt4 book ai didi

node.js - AlamofireImage 代码=-1016 "Failed to validate response due to unacceptable content type"

转载 作者:搜寻专家 更新时间:2023-11-01 00:30:48 25 4
gpt4 key购买 nike

我已经使用 Alamofire 和 SwityJSON 获取图像的 URL 来解析从 API 获得的响应。后来,在我的代码中,我尝试使用 AlamofireImage 将该图像设置为 ImageView ...我正在尝试执行以下操作:

let headers = ["Authorization": requestToken, "Content-Type": "image/jpg"]

print(foods[indexPath.row].image)

Alamofire.request(.GET, imageEndPoint, headers: headers)
.responseImage { response in
debugPrint(response)
print(response.request)
print(response.response)

if let image = response.result.value {
print("image downloaded: \(image)")
}
}

但是调试的时候出现了如下错误

 Request]: <NSMutableURLRequest: 0x7fa93e052e60> { URL: http://159.203.92.55:9000/api/media/image?path=./server/uploads/images/products/jOqmy768a5wN2tcPd07cPhVH.jpg }
[Response]: <NSHTTPURLResponse: 0x7fa93e05a0a0> { URL: http://159.203.92.55:9000/api/media/image?path=./server/uploads/images/products/jOqmy768a5wN2tcPd07cPhVH.jpg } { status code: 200, headers {
Connection = "keep-alive";
"Content-Type" = "image/jpg";
Date = "Sun, 17 Jan 2016 16:28:38 GMT";
"Transfer-Encoding" = Identity;
"X-Powered-By" = Express;
} }

[Data]: 26224 bytes
[Result]: FAILURE: Error Domain=com.alamofire.error Code=-1016 "Failed to validate response due to unacceptable content type" UserInfo={NSLocalizedFailureReason=Failed to validate response due to unacceptable content type}
Optional(<NSMutableURLRequest: 0x7fa93e052e60> { URL: http://159.203.92.55:9000/api/media/image?path=./server/uploads/images/products/jOqmy768a5wN2tcPd07cPhVH.jpg })

显然由于内容类型而出现错误,但我不确定如何解决:

[Result]: FAILURE: Error Domain=com.alamofire.error Code=-1016   "Failed to validate response due to unacceptable content type"  UserInfo={NSLocalizedFailureReason=Failed to validate response due to unacceptable content type}

因为这个错误,我无法继续,只能将图像设置到 imageView

   cell.thumbnailImageView.image = myImage

当我尝试使用 Chrome 的 Postman 执行相同的请求时,我得到的图像没有任何问题。

有什么建议吗?谢谢

更新:我找到了一种与我想要的相去甚远但有效的方法

  Alamofire.request(.GET, foods[indexPath.row].image, headers: headers)
.response { (request, response, data, error) in
debugPrint(response)
cell.thumbnailImageView.image = UIImage(data: data!, scale: 1)
}

这是 Vanilla Alamofire,所以我仍然无法使用 AlamofireImage,它为图像提供了更好的支持。我从以下帖子中获得了此方法:How to load image in swift using Alamofire (天窗的回答)

最佳答案

所以我注意到服务器的回答是 "Content-Type"= "image/jpg"; 但正如用户@Gomines 所指出的,alamofireImage 的正确内容类型实际上是 “内容类型”:“图像/jpeg”

然后我回到 AlamofireImages 文档并阅读了这个

If the image you are attempting to download is an invalid MIME type not in the list, you can add custom acceptable content types using the addAcceptableImageContentTypes extension on the Request type.

所以我只需要在 AlamofireImage 请求之前添加这行代码:

Request.addAcceptableImageContentTypes(["image/jpg"])

问题是我没有匹配服务器响应的相同内容类型。然而,该行解决了这个问题,因为现在 AlamofireImage 也将接受服务器发送的相同类型。

关于node.js - AlamofireImage 代码=-1016 "Failed to validate response due to unacceptable content type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34840735/

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