gpt4 book ai didi

image - 银联银行 : Can't I download image with a URL?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:25:16 25 4
gpt4 key购买 nike

我想从 URL 加载图像。

我的功能:

func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if "bigImage" == message.name {
print(message.body as? String ?? "nothing")
let urlString = message.body as! String
print(urlString)
imageView.downloadedFrom(link: urlString)
}
if imageView != nil{
}
}

我在以下位置遇到错误:imageView.downloadedFrom(link: urlString)

unexpectedly found nil while unwrapping an Optional value

即使我可以收到非零的消息。

顺便说一句,我打开了 ATS.. 真的很想知道发生了什么。

    func getDataFromUrl(url: URL, completion: @escaping (_ data: Data?, _  response: URLResponse?, _ error: Error?) -> Void) {
URLSession.shared.dataTask(with: url) {
(data, response, error) in
completion(data, response, error)
}.resume()
}

func downloadImage(url: URL) {
print("Download Started")
getDataFromUrl(url: url) { (data, response, error) in
guard let data = data, error == nil else { return }
print(response?.suggestedFilename ?? url.lastPathComponent)
print("Download Finished")
DispatchQueue.main.async() { () -> Void in
self.imageView.image = UIImage(data: data)
}
}
}

extension UIImageView {
func downloadedFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {
contentMode = mode
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
DispatchQueue.main.async() { () -> Void in
self.image = image
}
}.resume()
}
func downloadedFrom(link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) {
guard let url = URL(string: link) else { return }
downloadedFrom(url: url, contentMode: mode)
}

最佳答案

我建议使用图像加载器将图像加载到 UIImageView 中。使用 KFSwiftImageLoader

let imageView : UIImageView!
imageView.loadImage(urlString: imageURL)

关于image - 银联银行 : Can't I download image with a URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43183313/

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