2017-05-31 14 views
0

URL(downloadURL)を私のテーブルビューのセルに入る実際のイメージに変えたいと思います。取得するURLは、画像をダウンロードするためのリンクです。これは私がこれまで持っていたものです。ありがとう! viewDidLoad FUNCURLを画像として読み込むにはどうすればよいですか?

オーバーライド()を使用する{ 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することを学ぶ - あなたのpodfileで

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

を追加 -

ポッド 'SDWebImage'、 '〜> 3.8' を

関連する問題