菜鸟教程小白 发表于 2022-12-12 13:35:52

ios - 如何使用 swift 3.1 将图像从 url 加载到字典中作为数据


                                            <p><p>我对 swift 非常陌生,需要一些帮助来从 URL 中获取图像并将它们存储到字典中以引用到 UITableView。我检查了各种线程,但找不到满足特定需求的场景。</p>

<p>我目前将字典中的产品名称作为键,并将图像 URL 链接到每个名称:</p>

<pre><code>let productLibrary = [&#34;Product name 1&#34;:&#34;http://www.website.com/image1.jpg&#34;,
&#34;Product name 2&#34;:&#34;http://www.website.com/image2.jpg&#34;]
</code></pre>

<p>我需要将实际图像放入具有相同产品名称的字典中,作为添加到 UITableView 的键。</p>

<p>我目前使用以下代码直接在 tableView cellForRowAt 函数中加载图像,但这会使表格 View 无响应,因为它会在每次 TableView 刷新时加载图像:</p>

<pre><code>cell.imageView?.image = UIImage(data: try! Data(contentsOf: URL(string:
productLibrary]!)!))
</code></pre>

<p>mainPlaces 是 productLibrary 字典中列出的一系列产品的数组。最初在字典中预先加载图像肯定会减少加载时间并使 UITableView 像我需要的那样响应。</p>

<p>任何帮助将不胜感激!</p>

<p>@Samarth,我已经按照下面的建议实现了你的代码(只是将扩展直接复制到 ViewController 类上方的 ViewController.swift 文件的根目录中。</p>

<p>其余的,我已经粘贴在 ViewController 类的下面,但它实际上仍然没有在 tableview 中显示图像。</p>

<p>我已尝试完全按照您的建议进行操作,但也许我遗漏了一些明显的东西。很抱歉有很多回复,但我似乎无法让它工作。请在下面查看我的确切代码:</p>

<pre><code>internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: &#34;Cell&#34;)

    cell.textLabel?.text = mainPlaces

    downloadImage(url: URL(string: productLibrary]!)!)

    cell.imageView?.downloadedFrom(link: productLibrary]!)

    return cell

}

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    performSegue(withIdentifier: &#34;ProductSelect&#34;, sender: nil)

      globalURL = url]!

}

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

func downloadImage(url: URL) {
    print(&#34;Download Started&#34;)
    getDataFromUrl(url: url) { (data, response, error)in
      guard let data = data, error == nil else { return }
      print(response?.suggestedFilename ?? url.lastPathComponent)
      print(&#34;Download Finished&#34;)
      DispatchQueue.main.async() { () -&gt; Void in

            // self.imageView.image = UIImage(data: data)
            /* If you want to load the image in a table view cell then you have to define the table view cell over here and then set the image on that cell */
            // Define you table view cell over here and then write

            let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: &#34;Cell&#34;)

            cell.imageView?.image = UIImage(data: data)

      }
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以在项目中同步或异步加载图像。</p>

<p><strong>同步</strong>:表示你的数据正在主线程上加载,所以在你的数据被加载之前,你的主线程(UI Thread)将被阻塞。这就是您的项目中正在发生的事情</p>

<p><strong>异步</strong>:表示您的数据正在加载到不同于 UI 线程的其他线程上,因此 UI 不会被阻塞,并且您的数据加载是在后台完成的。</p>

<p>试试这个例子来<strong>异步加载图像</strong>:</p>

<p><strong>异步:</strong></p>

<p><strong>使用完成处理程序创建一个方法以从您的 url 获取图像数据</strong></p>

<pre><code>func getDataFromUrl(url: URL, completion: @escaping (_ data: Data?, _response: URLResponse?, _ error: Error?) -&gt; Void) {
    URLSession.shared.dataTask(with: url) {
      (data, response, error) in
      completion(data, response, error)
    }.resume()
}
</code></pre>

<p><strong>创建下载图片的方法(启动任务)</strong></p>

<pre><code>func downloadImage(url: URL) {
    print(&#34;Download Started&#34;)
    getDataFromUrl(url: url) { (data, response, error)in
      guard let data = data, error == nil else { return }
      print(response?.suggestedFilename ?? url.lastPathComponent)
      print(&#34;Download Finished&#34;)
      DispatchQueue.main.async() { () -&gt; Void in

         // self.imageView.image = UIImage(data: data)
   /* If you want to load the image in a table view cell then you have to define the table view cell over here and then set the image on that cell */
         // Define you table view cell over here and then write
         //      cell.imageView?.image = UIImage(data: data)

      }
    }
}
</code></pre>

<p><strong>用法:</strong></p>

<pre><code>override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    print(&#34;Begin of code&#34;)
    if let checkedUrl = URL(string: &#34;your image url&#34;) {
      imageView.contentMode = .scaleAspectFit
      downloadImage(url: checkedUrl)
    }
    print(&#34;End of code. The image will continue downloading in the background and it will be loaded when it ends.&#34;)
}
</code></pre>

<p><strong>扩展:</strong></p>

<pre><code>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(&#34;image&#34;),
                let data = data, error == nil,
                let image = UIImage(data: data)
            else { return }
            DispatchQueue.main.async() { () -&gt; 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)
    }
}
</code></pre>

<p><strong>用法:</strong></p>

<pre><code>imageView.downloadedFrom(link: &#34;image url&#34;)
</code></pre>

<p><strong>关于您的问题</strong></p>

<p>在表格 View 单元格中加载图像时执行此操作:</p>

<pre><code> cell.imageView?.downloadedFrom(link: productLibrary]! )
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何使用 swift 3.1 将图像从 url 加载到字典中作为数据,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45194878/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45194878/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何使用 swift 3.1 将图像从 url 加载到字典中作为数据