【发布时间】:2017-09-15 20:08:29
【问题描述】:
您好,您可以使用以下代码从 Web 服务器下载图像。该请求是 HTTP,因此不安全。
DispatchQueue.global(qos: .userInitiated).async {
var productImage = Utilities.GetEmptyImageBottle()
if (entry.ImageName != nil || entry.ImageUrl != nil) {
let data = NSData(contentsOf: URL(string: entry.ImageUrl.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!)!)
if data != nil {
let image = UIImage(data: data! as Data)
productImage = image!
}
}
DispatchQueue.main.async {
cell.ProductImage.image = productImage
}
}
我还在 info.plist 中添加了以下内容
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
现在我的图片无法加载,并且出现以下错误:
NSURLConnection finished with error - code -1002
这是在运行 iOS 11 的 Xcode 9 中。在 Xcode 8 中的 iOS 10 上,没有问题。
我错过了什么?
【问题讨论】:
-
除了问题之外,您的代码有很多问题。不要使用
nil检查,而是使用if let语句。此外,您应该使用NSURLSession.异步加载 Web 请求
标签: ios swift web-services nsurl