Ios 带有UMLAUT的图像url写入了错误的部分
我使用Firebase做了一些简单的任务,将图像Ios 带有UMLAUT的图像url写入了错误的部分,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,我使用Firebase做了一些简单的任务,将图像url设置为json,下载并显示。但是由于某种原因,如果url包含umlauts,例如(ö,ä,ü等),则url设置为错误的部分 编辑:我必须澄清我使用url进行百分比编码。所以它实际上不包含任何UMLAUT 所以url实际上看起来像这样,例如Göteborgs RapéLoose.png: 使用umlaut时,它是这样做的: 如果没有umlauts,它会纠正如下情况: 这就是我如何完成我描述的所有这些步骤: if productsValue[
url
设置为json
,下载并显示。但是由于某种原因,如果url
包含umlauts,例如(ö,ä,ü等),则url设置为错误的部分
编辑:我必须澄清我使用url进行百分比编码。所以它实际上不包含任何UMLAUT
所以url
实际上看起来像这样,例如Göteborgs RapéLoose.png
:
使用umlaut时,它是这样做的:
如果没有umlauts,它会纠正如下情况:
这就是我如何完成我描述的所有这些步骤:
if productsValue[indexPath.row]["productUrl"] != nil {
cell.snusProductImageView!.kf_setImageWithURL(NSURL(string: productsValue[indexPath.row]["productUrl"] as! String)!)
}
else {
let productImageref = productsValue[indexPath.row]["Products"] as! String
let decomposedPath = productImageref.decomposedStringWithCanonicalMapping
cell.snusProductImageView.image = nil
cell.snusProductImageView.kf_showIndicatorWhenLoading = true
FIRStorage.storage().reference().child("\(decomposedPath).png").downloadURLWithCompletion({(url, error)in
FIRDatabase.database().reference().child("Snuses").child(decomposedPath).child("productUrl").setValue(url!.absoluteString)
let resource = Resource(downloadURL: url!, cacheKey: decomposedPath)
cell.snusProductImageView.kf_setImageWithURL(url)
})
有什么问题,你能告诉我吗?我已经研究这个问题好一个星期了 URL不能直接包含“Umlauts”。简单地说,URL是一系列受限制的ASCII字符 这意味着,不允许的字符(例如,
ö
、ä
、ü
等)需要正确编码。由于URL由几个不同的组件组成,每个组件可能使用稍微不同的编码。此处介绍了详细信息:
在Cocoa和Cocoa Touch中,有一个helper类,它可以让您轻松地从给定的原始组件以自然字符编码的方式组成适当的URL字符串
请注意,仅对整个不正确编码的URL应用“百分比编码”通常会产生另一个不正确编码的URL。不要那样做!使用nsurl组件
编辑:
让我对您的代码应用一些(希望有用的)注释:
这个表达有问题:
NSURL(string:productsValue[indexPath.row][“productUrl”]as!string)代码>
“productUrl”
处可能缺少的值显式初始化URL
,并包括正确的错误处理。众所周知,将字符串转换为URL容易出错
然后
让decomposedPath=productImageref.decomposedStringWithCanonicalMapping
URL
类型的值),并在通过网络发送时将其重新正确转换为字符串,分别转换为ASCII字符数组
编辑2:
下面的代码演示了如何使用NSURLComponents
从给定的基本URL、引用端点的部分路径组件和给定的参数集安全地创建URL
把它复制/复制到操场上试用
import Cocoa
extension String: ErrorType {}
// First, create a base URL and save it later for reuse:
// Caution: The given string must be properly encoded! But usually, the base address
// in native form should only use allowed characters anyway, so the encoded form
// should be equal to the native form (otherwise, you have to encode it per component).
let urlBaseString = "https://firebasestorage.googleapis.com/v0/b/snuspedia.appspot.com/o"
guard let urlBase = NSURL(string: urlBaseString) else {
throw "bad base URL"
}
print("Base URL path: \"\(urlBase.path!)\"")
// Get the current path and parameters using strings using their "native character set":
let itemPath = "Göteborgs Rapé Loose.png"
let params = ["alt": "media", "token": "e8f2219b-d14e-46f6-a90f-ee21f912af6c"]
// Create a partial URL components (representing a relative URL) with the given
// path and query component:
let components = NSURLComponents()
components.path = itemPath
components.queryItems = params.map { NSURLQueryItem(name: $0, value: $1) }
// Combine the base URL with the partial URL in order to get a fully specified URL:
guard let url = components.URLRelativeToURL(urlBase) else {
throw "Failed to create final URL"
}
print("URL: \(url.absoluteString)")
输出:
URL:https://firebasestorage.googleapis.com/v0/b/snuspedia.appspot.com/G%C3%B6teborgs%20Rap%C3%A9%20Loose.png?alt=media&token=e8f2219b-d14e-46f6-a90f-ee21f912af6c
URL不能直接包含“Umlauts”。简单地说,URL是一系列受限制的ASCII字符
这意味着,不允许的字符(例如,ö
、ä
、ü
等)需要正确编码。由于URL由几个不同的组件组成,每个组件可能使用稍微不同的编码。此处介绍了详细信息:
在Cocoa和Cocoa Touch中,有一个helper类,它可以让您轻松地从给定的原始组件以自然字符编码的方式组成适当的URL字符串
请注意,仅对整个不正确编码的URL应用“百分比编码”通常会产生另一个不正确编码的URL。不要那样做!使用nsurl组件
编辑:
让我对您的代码应用一些(希望有用的)注释:
这个表达有问题:
NSURL(string:productsValue[indexPath.row][“productUrl”]as!string)代码>
“productUrl”
处可能缺少的值显式初始化URL
,并包括正确的错误处理。众所周知,将字符串转换为URL容易出错
然后
让decomposedPath=productImageref.decomposedStringWithCanonicalMapping
URL
类型的值),并在通过网络发送时将其重新正确转换为字符串,分别转换为ASCII字符数组
编辑2:
下面的代码演示了如何使用NSURLComponents
从给定的基本URL、引用端点的部分路径组件和给定的参数集安全地创建URL
把它复制/复制到操场上试用
import Cocoa
extension String: ErrorType {}
// First, create a base URL and save it later for reuse:
// Caution: The given string must be properly encoded! But usually, the base address
// in native form should only use allowed characters anyway, so the encoded form
// should be equal to the native form (otherwise, you have to encode it per component).
let urlBaseString = "https://firebasestorage.googleapis.com/v0/b/snuspedia.appspot.com/o"
guard let urlBase = NSURL(string: urlBaseString) else {
throw "bad base URL"
}
print("Base URL path: \"\(urlBase.path!)\"")
// Get the current path and parameters using strings using their "native character set":
let itemPath = "Göteborgs Rapé Loose.png"
let params = ["alt": "media", "token": "e8f2219b-d14e-46f6-a90f-ee21f912af6c"]
// Create a partial URL components (representing a relative URL) with the given
// path and query component:
let components = NSURLComponents()
components.path = itemPath
components.queryItems = params.map { NSURLQueryItem(name: $0, value: $1) }
// Combine the base URL with the partial URL in order to get a fully specified URL:
guard let url = components.URLRelativeToURL(urlBase) else {
throw "Failed to create final URL"
}
print("URL: \(url.absoluteString)")
输出:
URL:https://firebasestorage.googleapis.com/v0/b/snuspedia.appspot.com/G%C3%B6teborgs%20Rap%C3%A9%20Loose.png?alt=media&token=e8f2219b-d14e-46f6-a90f-ee21f912af6c
URL首先不应包含任何UMLAUT;它们应该是百分比编码的,我按照代码中的分解路径进行编码。它将删除这些。实际上,在firebase中,它们不包含umlauts或unicode,但在手册中有百分比编码。我不认为URL/百分比编码是decomposedStringWithCanonicalMapping
所做的。它看起来像是将u
之类的东西规范化为u
。您是否已检查结果URL并尝试手动调用它以查看返回的结果?downloadURLWithCompletion
中没有错误?(iO)