简体   繁体   English

从 Firebase 中检索图像 放置在 tableView 中的存储不起作用,

[英]Retrieving image from Firebase Storage placing in tableView not working,

I was able to display the image properly when i have just an imageView in a ViewController.当我在 ViewController 中只有一个 imageView 时,我能够正确显示图像。 I used this code:我使用了这段代码:

        islandRef.getData(maxSize: 1 * 1024 * 1024) { [weak self] data, error in
          if let error = error {
            print((error.localizedDescription))
            // Uh-oh, an error occurred!
          }
            if let data = data{
                self?.imageItem.image = UIImage(data: data)
            }

        }
    }

I try a similar approach for my tableView, but its a little more complicated.我为我的 tableView 尝试了类似的方法,但它有点复杂。 I have two arrays that pull a string and an integer from my firebase document.我有两个 arrays 从我的 firebase 文档中拉出一个字符串和一个 integer。 I append these to the arrays items and prices.我 append 这些到 arrays 项目和价格。 I am able to show these values in my tableView.我能够在我的 tableView 中显示这些值。 I try the same thing when my pictues array.当我的图片数组时,我尝试同样的事情。 I am able to append an image to it.我可以 append 给它一个图像。 I then check if the pictures array has a count.然后我检查图片数组是否有计数。 It has a count of 1, but when i try to access it in tableview.它的计数为 1,但是当我尝试在 tableview 中访问它时。 It says the error: Thread 1: Fatal error: Index out of range.它说错误:线程1:致命错误:索引超出范围。 I don't understand why my other arrays have values that are usable, but this array does.我不明白为什么我的其他 arrays 有可用的值,但这个数组有。 I don't think there is a problem with the imageView because i can replaces pictures[indexPath.row] with my PlaceholderImage and it will properly show my placeholder image.我认为 imageView 没有问题,因为我可以用我的 PlaceholderImage 替换图片[indexPath.row],它会正确显示我的占位符图像。

class ProfileViewController: UITableViewController {
    var item = [String]()
    var prices = [Int]()
    var pricePicture = [Any]()
    var db:Firestore!
    var pictures = [UIImage]()
    let storage = Storage.storage()
    var placeholderImage = UIImage(named: "placeholder.jpg")
    var imageMenu:UIImage?

    override func viewDidLoad() {
        getData()


        let db = Firestore.firestore()
        super.viewDidLoad()

    }
    func getData(){

        let db = Firestore.firestore()
        let docRef = db.collection("wine").document("pinot-noir-2017")
        let storage = Storage.storage()
               docRef.getDocument(source: .server) { (document, error) in

                if let document = document {
                    let keys = document.data()?.keys
                       for key in keys!{
                        self.item.append(key)
                        self.pricePicture = document.data()![key] as! [Any]
                        self.prices.append(self.pricePicture[0] as! Int)


                        let stor = storage.reference()
                        let islandRef = stor.child("carbanet.jpg")
                        islandRef.getData(maxSize: 1 * 1024 * 1024) { [weak self] data, error in
                          if let error = error {
                            print((error.localizedDescription))
                          }
                            if let data = data{

                                self?.pictures.append(UIImage(data: data)!)

                                //this will print 1
                                print(self?.pictures.count ?? 0)
                            }
                        }

                        self.tableView.reloadData()

                        }
                   } else {
                       //print("Document does not exist in cache")
               }
        }
    }




    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return item.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Picture", for: indexPath)
        cell.textLabel?.text = item[indexPath.row] + "  $" + String(prices[indexPath.row])

        cell.imageView?.image = pictures[indexPath.row]

        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let detailViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.detailViewController) as? DetailViewController{
            show(detailViewController, sender: .none)
        }

    }
}

If someone could show me the error I am making that would be great.如果有人能告诉我我正在犯的错误,那就太好了。 I'm pretty new to swift, so any help you can give me would be appreciated.我对 swift 还很陌生,所以您能给我的任何帮助将不胜感激。 I have read through the firebase documentation and watched their videos, but I can't figure out why it works in one scenario and not the other.我已通读 firebase 文档并观看了他们的视频,但我不明白为什么它在一种情况下有效,而在另一种情况下无效。

I think it was because i was trying to do it inside of trying to access a firebase document.我认为这是因为我试图在试图访问 firebase 文档的过程中这样做。 I made another function getImage() and this time it worked.我又做了一个 function getImage() ,这一次成功了。

    func getImage(){

    let stor = storage.reference()
    let islandRef = stor.child("carbanet.jpg")

    islandRef.getData(maxSize: 1 * 1024 * 1024) { [weak self] data, error in
      if let error = error {
        print((error.localizedDescription))
        // Uh-oh, an error occurred!
      }
        if let data = data{

            self?.pictures.append(UIImage(data: data as Data)!)

            //this will print 1
            print(self?.pictures.count ?? 0)
        }
    }
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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