简体   繁体   English

从Firebase检索图像

[英]Retrieving Image From Firebase

The app run successfully , but the image did not show up in the table cell. 该应用程序成功运行,但是该图像未显示在表格单元格中。

let dbRef = FIRDatabase.database().reference().child("restaurants/restaurantImage")
    dbRef.observe(.childAdded, with: {(snapshot) in
        let downloadURL = snapshot.value as! String
        let storageRef = FIRStorage.storage().reference(forURL: downloadURL)
        storageRef.data(withMaxSize: 10 * 1024 * 1024) { (data, error) -> Void in
            let pic = UIImage(data: data!)
            self.picArray.append(pic!)

        }
        self.tableViewHongKong.reloadData()
    })

You should move self.tableViewHongKong.reloadData() inside your completion handler. 您应该将self.tableViewHongKong.reloadData()移到完成处理程序中。 With your current code you reload the table before the asynchronous function would finish. 使用当前代码,您需要在异步功能完成之前重新加载表。

You shouldn't do force unwrapping unless you are 100% sure that data will actually return and that the UIImage initializer will succeed. 除非您100%确保实际返回数据并且UIImage初始化成功,否则不要强行解包。

let dbRef = FIRDatabase.database().reference().child("restaurants/restaurantImage")
    dbRef.observe(.childAdded, with: {(snapshot) in
        let downloadURL = snapshot.value as! String
        let storageRef = FIRStorage.storage().reference(forURL: downloadURL)
        storageRef.data(withMaxSize: 10 * 1024 * 1024) { (data, error) -> Void in
            guard let imageData = data, let pic = UIImage(data: imageData) else { return }
            self.picArray.append(pic)
            self.tableViewHongKong.reloadData()
        }
    })

The images do not appear because you are reloading your table view before any pictures are appended to the array. 该图像未出现,因为在将任何图片添加到数组之前您正在重新加载表格视图。 The data(withMaxSize: starts an asynchronous request that gets a response after you have reloaded the table view. data(withMaxSize:启动一个异步请求,该请求在重新加载表视图后将获得响应。

If you move self.tableViewHongKong.reloadData() inside the data(withMaxSize: response block, you will reload the table view after you append each successfully loaded image. 如果将self.tableViewHongKong.reloadData()移到data(withMaxSize:响应块self.tableViewHongKong.reloadData()内,则在追加每个成功加载的图像后,将重新加载表视图。

Even if Callam already answered your question, I am still posting this since it is probably better way for doing it. 即使Callam已经回答了您的问题,我仍然在发布此消息,因为这可能是更好的解决方法。

Instead of writing all that code just to display one image, you can use Glide which works very well with Firebase plus it is well documented in Firebase Docs. 您可以使用Glide与Firebase搭配使用,并且在Firebase Docs中有很好的文档说明,而不必编写所有代码仅显示一个图像。 Check it out, it made everything a lot easier for me when I started using it. 检查一下,当我开始使用它时,它对我来说使一切变得容易得多。

https://firebase.google.com/docs/storage/android/download-files#downloading_images_with_firebaseui https://firebase.google.com/docs/storage/android/download-files#downloading_images_with_firebaseui

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

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