来自Firebase的图像未显示在TableView单元格中[Swift]

时间:2018-08-28 15:56:18

标签: swift image uitableview firebase

我试图在TableView单元格中显示Firebase数据库中的图像,但是,当我运行应用程序时,该单元格显示为空白。一切似乎都连接到情节提要,并且Firebase已连接(在数据库中,对图像的引用标记为“ imageURL”),因此我假设这是代码本身的问题。代码如下所示:

Post.swift

import Foundation
import Firebase
import FirebaseStorage
import FirebaseDatabase

class Post {
    private var _image: String!
    private var _imageKey: String!
    private var _imageRef: FIRDatabaseReference!

    var postImg: String {
      get {
         return _image
      } set {
        _image = newValue
      }
    }

    var imageKey: String {
        return _imageKey
    }


    init(imgUrl: String) {
        _image = imgUrl
    }

    init(imageKey: String, imageData: Dictionary<String, AnyObject>) {
        _imageKey = imageKey

        if let postImage = imageData["imageUrl"] as? String {
            _image = postImage
         }
        _imageRef = 
        FIRDatabase.database().reference().child("images").child(_imageKey)
    }

}

PostCell.swift

import UIKit
import Firebase
import FirebaseDatabase

    class PostCell: UITableViewCell {

    @IBOutlet weak var postImg: UIImageView!

    var searchedPost: Post!
    var searchedPostKey: FIRDatabaseReference!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

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

        // Configure the view for the selected state
    }

    func configCell(searchedPost: Post, img: UIImage? = nil) {
        self.searchedPost = searchedPost

        if img != nil {
            self.postImg.image = img
        } else {

            let ref = FIRStorage.storage().reference(forURL:  searchedPost.postImg)
            ref.data(withMaxSize: 10 * 1000, completion: { (data, error) in

                if error != nil {
                    print(error) 
                } else {  
                    if let imgData = data {                         
                        if let img = UIImage(data: imgData){ 
                            self.postImg.image = img
                        }
                    }
                }
            })
        }

    }

}

ViewController.swift

import UIKit
import Firebase
import FirebaseDatabase

    class LeftView: UIViewController, UITableViewDelegate, UITableViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var tableView: UITableView!

    var images = [Post]()
    var searchedPost: Post!
    var imagePicker: UIImagePickerController!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
        imagePicker = UIImagePickerController()
        imagePicker.allowsEditing = true
        imagePicker.delegate = self

        FIRDatabase.database().reference().child("images").observe(.value, with:
        {(snapshot) in
            if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {

                self.images.removeAll()

                for data in snapshot {
                     print(data)
                    if let imageDict = data.value as? Dictionary<String, AnyObject> {
                        let key = data.key
                        let searchedPost = Post(imageKey: key, imageData: imageDict)
                        self.images.append(searchedPost) 
                    }
                }
            }
                self.tableView.reloadData()
        })
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let searchedPost = images[indexPath.row]
        if let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as? PostCell {
            cell.configCell(searchedPost: searchedPost)
            return cell
        } else {
            return PostCell()
        }
    }
}

1 个答案:

答案 0 :(得分:0)

从Firebase获取图像后,您需要重新加载tableview。 添加此行

self.tableView.reloadData()

之后

self.images.append(searchedPost)

还需要在“主队列”中设置图像。在“配置单元格”方法中使用此

DispatchQueue.main.async {
      self.postImg.image = img
}

代替

self.postImg.image = img