Swift:从url检索图像时出错

问题描述 投票:0回答:1

我正在尝试从网址中检索图像但由于某种原因它不会显示图像

if let url = NSURL(string: "http://cdn.businessoffashion.com/site/uploads/2014/09/Karl-Lagerfeld-Self-Portrait-Courtesy.jpg") {
    if let data = NSData(contentsOfURL: url) {
        print("work")
        imageUIView.image = UIImage(data: data)
    } else {
        print("dontwork")
    }
}
swift
1个回答
1
投票

如果您的目标是iOS 9.0或OS X v10.11,则可能会遇到Apple的App Transport Security。在针对这些操作系统版本的同时运行代码,将在控制台中显示以下输出:

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

Apple now requires你只能通过https访问文件或通过http允许非https(即.info.plist)例外。

如果你还没有,我建议更新你的info.plist

您可以基于每个域执行此操作,也可以允许任意加载非https URL。

例如,使用以下键/值更新info.plist

enter image description here

如果这是您的问题,一旦您的info.plist更新,您的代码应该工作。

© www.soinside.com 2019 - 2024. All rights reserved.