我试图从我的localhost中的URL捕获图像,但不知何故它给出了一个错误
import UIKit
let path = "http://localhost/Twitter/Post/52/POST-BCFE0D62-CE3F-407B-B510-CC3B9A8372CF.jpeg"
let url = URL(string: path)!
do {
let imageData = try Data(contentsOf: url)
let image = UIImage(data: imageData)
} catch {
print("error \(error)")
}
这是我操场上的结果。这里出了什么问题:(
错误信息 :
error Error Domain = NSCocoaErrorDomain Code = 256“无法打开文件”POST-BCFE0D62-CE3F-407B-B510-CC3B9A8372CF.jpeg“。的UserInfo = {NSURL = http://localhost/Twitter/Post/52/POST-BCFE0D62-CE3F-407B-B510-CC3B9A8372CF.jpeg}
Swift 3.0
let url = URL(string: "http://i.imgur.com/w5rkSIj.jpg")
let data = try? Data(contentsOf: url)
if let imageData = data {
let image = UIImage(data: data)
}
使用上面的代码从url获取图像。希望它会对你有所帮助。