如何从Firebase检索图像并在tableView中显示它们?

时间:2017-07-23 19:32:58

标签: ios swift firebase firebase-realtime-database firebase-storage

我正在使用Xcode 8,Swift 3和Firebase制作iOS应用。我想从Firebase中检索所有用户,并在tableViewCell中显示他们的姓名和个人资料图片。

控制器

import UIKit
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage
import Firebase

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource  {


    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!

    var posts = NSMutableArray()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.delegate = self
        self.tableView.dataSource = self


        loadData()

    }

    func loadData() {
        activityIndicatorView.startAnimating()
        Database.database().reference().child("users").observeSingleEvent(of: .value, with: { (snapshot) in
            if let postsDictionary = snapshot.value as? [String : AnyObject] {
                for post in postsDictionary {
                    self.posts.add(post.value)
                }

                self.tableView.reloadData()

            }
        })
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.posts.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell
        // Configure the cell...
        let user = self.posts[indexPath.row] as! [String : AnyObject]
        cell.nameLabel.text = user["username"] as? String
        if let imageName = user["profile_image"] as? String {
            let imageRef = Storage.storage().reference().child("profile_image/\(imageName)")
            imageRef.getData(maxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
                if error == nil {
                    // successful
                    let image = UIImage(data: data!)
                    cell.profileImageView.image = image
                    cell.profileImageView.alpha = 1
                    cell.nameLabel.alpha = 1


                    } else {
                    // error
                    print(error!.localizedDescription)
                }
            })
        }

        activityIndicatorView.stopAnimating()
        return cell
    }

}

TableViewCell

import UIKit
import FirebaseDatabase
import FirebaseAuth
import SDWebImage

class HomeTableViewCell: UITableViewCell {


@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var profileImageView: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()

    self.nameLabel.alpha = 1
    self.profileImageView.alpha = 1

}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}

}

当我运行它时,名称会显示,但用户的个人资料图片不会显示。这是我的Firebase存储: enter image description here

我的Firebase数据库看起来像这样:

enter image description here

0 个答案:

没有答案