2017-05-31 72 views
0

我想将URL(downloadURL)转换为一个实际的图像,它将放入我的表格视图单元格中。它将检索的URL是下载图像的链接。这是我迄今为止所拥有的。谢谢!如何将网址加载为图片?

倍率FUNC viewDidLoad中(){ super.viewDidLoad()

let ref = FIRDatabase.database().reference().child("Posts") 

    ref.observeSingleEvent(of: .value, with: { snapshot in 

     print(snapshot.childrenCount) 

     for rest in snapshot.children.allObjects as! [FIRDataSnapshot] { 

      guard let value = rest.value as? Dictionary<String,Any> else { continue } 

      guard let title = value["Title"] as? String else { continue } 

      guard let date = value["Date"] as? String else { continue } 

      guard let author = value["Author"] as? String else { continue } 

      guard let article = value["Article"] as? String else { continue } 

      guard let downloadURL = value["DownloadURL"] as? String else { continue } 

      let post = postStruct(title: title, date: date, author: author, article: article, downloadURL: downloadURL) 

      self.posts.append(post) 

     } 

     self.posts = self.posts.reversed(); self.tableView.reloadData() 

    }) 


} 






override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return posts.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 

    let label1 = cell?.viewWithTag(1) as! UILabel 
    label1.text = posts[indexPath.row].title 
    return cell! 
} 

}

回答

0

一个流行的库使用是 -

https://github.com/rs/SDWebImage

这将处理图像加载为你使用 -

imageView.sd_setImage(with: URL(string: downloadURL), placeholderImage: UIImage(named: "placeholder.png"))

您可以在自己的占位符添加或不使用任何。我相信它也会为你处理缓存。

要了解如何设置的CocoaPods这个库的后续 -

https://guides.cocoapods.org/using/getting-started.html

在你podfile加 -

荚 'SDWebImage', '〜> 3.8'