简体   繁体   中英

Retrieve image from firebase storage to show on tableView cell

I just need help I building Instagram-clone with firebase and I have an issue whit post feed I can't Retrieve image from firebase storage to show on tableView cell can you help me, please :(

import UIKit

import FirebaseAuth

import FirebaseDatabase

class HomeViewController: UIViewController ,UITableViewDelegate {

@IBOutlet weak var tableview: UITableView!
var posts = [Post]()
override func viewDidLoad() {
    super.viewDidLoad()

    tableview.dataSource = self
    loadposts()

 //   var post = Post(captiontxt: "test", photoUrlString: "urll")
 //   print(post.caption)
 //   print(post.photoUrl)

}

func loadposts() {
    Database.database().reference().child("posts").observe(.childAdded){ (snapshot: DataSnapshot)in
        print(Thread.isMainThread)
          if let dict = snapshot.value  as? [String: Any]{
            let captiontxt = dict["caption"] as! String
            let photoUrlString = dict["photoUrl"] as! String
           let post = Post(captiontxt: captiontxt, photoUrlString: photoUrlString )
            self.posts.append(post)
            print(self.posts)
            self.tableview.reloadData()
        }
    }
}

@IBAction func logout(_ sender: Any) {
    do {
        try Auth.auth().signOut()
    }catch let logoutErrorr{
        print(logoutErrorr)
    }
    let storyboard = UIStoryboard(name: "Start", bundle: nil)
    let signinVC = storyboard.instantiateViewController(withIdentifier: "SigninViewController")
    self.present(signinVC, animated: true, completion: nil)


}

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

}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableview.dequeueReusableCell(withIdentifier: "imagecell", for: indexPath) as! PostCellTableViewCell
   cell.captionLabel.text = posts[indexPath.row].caption
    cell.postimage.image =  posts[indexPath.row].photoUrl
   // print(cell.captionLabel.text)
   // print(cell.daysLabel.text)


    return cell
}

}

enter code here

import Foundation class Post { var caption: String var photoUrl: String

init(captiontxt: String, photoUrlString: String) {
    caption = captiontxt
    photoUrl = photoUrlString

}

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableview.dequeueReusableCell(withIdentifier: "imagecell", for: indexPath) as! PostCellTableViewCell

    cell.postimage.image = nil

    cell.tag += 1
    let tag = cell.tag

    cell.captionLabel.text = posts[indexPath.row].caption

    let photoUrl = posts[indexPath.row].photoUrl

    getImage(url: photoUrl) { photo in
        if photo != nil {
            if cell.tag == tag {
                DispatchQueue.main.async {
                    cell.postimage.image = photo
                }
            }
        }
    }

    return cell
}

func getImage(url: String, completion: @escaping (UIImage?) -> ()) {
    URLSession.shared.dataTask(with: URL(string: url)!) { data, response, error in
        if error == nil {
            completion(UIImage(data: data!))
        } else {
            completion(nil)
        }
    }.resume()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM