将字符串转换为 NSURL returns 无

Casting String to NSURL returns nil

我正在尝试从 url 获取图像,如下所示:

var string = "https://maps.googleapis.com/maps/api/staticmap?key=AIzaSyAJeHxZaZuNsYimNCJ4r0yuO-OYM8cINOI&center=6.241381,-75.595083&zoom=13&size=600x300&path=color:0x0000ff|weight:5|6.241381,-75.595083&signature=u7sM3m2h-qFSJoARR7cqD0CSBvU="

let url = NSURL(string: string.stringByRemovingPercentEncoding!)

if let data = NSData(contentsOfURL: url){
      let image = UIImage(data: data)
}

但是 returns 没有 url。 url 似乎不存在,但它确实存在,如果您转到 url,您将看到一个图像。

演员表有什么问题?或者我怎样才能得到这张图片。

这就是我使用 AlamofireImage

在我的应用程序中加载图像的方式
let url = NSURL(string:"someUrl")
                self.imgView.af_setImageWithURL(url!, placeholderImage: nil, filter: nil,  imageTransition: .CrossDissolve(0.5), runImageTransitionIfCached: false, completion:  { (error) in
                // do Something when asynchronous image loading is completed
            })

编辑 您的 url 包含 |这对 url 无效,因此您可以使用 PercentEncoding。

这是一个例子

let strurl = "https://maps.googleapis.com/maps/api/staticmap?key=AIzaSyAJeHxZaZuNsYimNCJ4r0yuO-OYM8cINOI&center=6.241381,-75.595083&zoom=13&size=600x300&path=color:0x0000ff|weight:5|6.241381,-75.595083&signature=u7sM3m2h-qFSJoARR7cqD0CSBvU="
let myURL = NSURL(string: strurl.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!)
self.imgView.af_setImageWithURL(myURL!, placeholderImage: nil, filter: nil,  imageTransition: .CrossDissolve(0.5), runImageTransitionIfCached: false, completion:  { (error) in
            // do Something when asynchronous image loading is completed
        })

问题出在您 URL 的查询部分中的竖线 (|)。它们应该转义为 %7C.

以下是我将如何构建自动转义查询部分的 URL:

var components = NSURLComponents(string: "https://maps.googleapis.com/maps/api/staticmap")!
components.query = "key=AIzaSyAJeHxZaZuNsYimNCJ4r0yuO-OYM8cINOI&center=6.241381,-75.595083&zoom=13&size=600x300&path=color:0x0000ff|weight:5|6.241381,-75.595083&signature=u7sM3m2h-qFSJoARR7cqD0CSBvU="

if let url = components.url {
    // ...
}

如果你使用的是Swift3,NSURLComponents重命名为URLComponents

您必须按如下方式向查询添加百分比编码:

Swift 2.3

let domainAPI = "https://maps.googleapis.com/maps/api/staticmap?"
let query = "key=AIzaSyAJeHxZaZuNsYimNCJ4r0yuO-OYM8cINOI&center=6.241381,-75.595083&zoom=13&size=600x300&path=color:0x0000ff|weight:5|6.241381,-75.595083&signature=u7sM3m2h-qFSJoARR7cqD0CSBvU="
if let queryPercentEncoded = query.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet()),
    let url = NSURL(string: domainAPI + queryPercentEncoded) {
    print(url)
}

Swift 3

if let queryPercentEncoded = query.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
    let url = URL(string: domainAPI + queryPercentEncoded) {
    print(url)
}

(我没有使用过基于密钥的认证,所以有些部分可能需要修改。)

首先,您需要一个正确的百分号转义 URL 字符串,不带签名:

let key = "AI...OI" // better hide your actual key in a public space...
let rawURLString = "https://maps.googleapis.com/maps/api/staticmap?key=\(key)&center=6.241381%2B-75.595083&zoom=13&size=600x300&path=color%3A0x0000ff%7Cweight%3A5%7C6.241381%2B-75.595083"
//`rawURLString` needs to be a valid string representation of URL.
assert(NSURL(string: rawURLString) != nil)

然后,为 URL 字符串生成一个 base64 编码的签名:

let encodedSignature: String = generateEncodedSignature(rawURLString, secret: yourSecretKey)

之后,您可以创建一个带符号的 URL 字符串:

let signedURLString = "\(rawURLString)&signature=\(encodedSignature)"

(我相信你有一些功能可以在你的应用程序中生成签名或签名url,根据它修改上面的代码。)

signedURLString 的内容应该是这样的:

https://maps.googleapis.com/maps/api/staticmap?key=AI...OI&center=6.241381%2B-75.595083&zoom=13&size=600x300&path=color%3A0x0000ff%7Cweight%3A5%7C6.241381%2B-75.595083&&signature=u7...=

(signature 不能以 "u7" 开头。)

并使用 signedURLString 创建 NSURL。不要删除百分比编码。

let url = NSURL(string: signedURLString)