gpt4 book ai didi

swift - 加载url图片

转载 作者:行者123 更新时间:2023-11-30 12:08:43 25 4
gpt4 key购买 nike

我是 IOS 开发新手,我正在尝试从 URL 加载图像,我知道 swift 版本之间存在一些变化。由于某种原因,我得到 imageData = nil 并且我不知道为什么..

 private func fetchImage() 
{
let url = URL(fileURLWithPath: "https://zgab33vy595fw5zq-zippykid.netdna-ssl.com/wp-content/uploads/2017/09/blog_1280x720.png")
if let imageData = NSData(contentsOf: url as URL){
image = UIImage(data: imageData as Data)
}
}

最佳答案

请检查:

private func fetchImage() {
let url = URL(string: "https://zgab33vy595fw5zq-zippykid.netdna-ssl.com/wp-content/uploads/2017/09/blog_1280x720.png")!
let task = URLSession(configuration: .default).dataTask(with: url) { (data, response, error) in
if error != nil {
print("Error Occurred: \(String(describing: error))")
}
else {
if let imageData = data {
let image = UIImage(data: imageData)
} else {
print("Image file is currupted")
}
}
}
task.resume()
}

关于swift - 加载url图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46342802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com