当前位置: 首页 > 面试题库 >

从Firebase存储中检索图像以显示在tableView(Swift)上

丌官炎彬
2023-03-14
问题内容

New Firebase的扩展问题检索数据并将其放在tableView(Swift)

通过将Firebase代码移动到viewDidLoad,Post的所有文本部分都可以显示出来。但是我仍然不能将图像放到tableView中。我已经检查了图像检索,并成功了,我想我从Firebase检索的图像不在发布数组中。

这是我的修复代码:

  import UIKit
  import Firebase
  import FirebaseStorage

class timelineTableViewController: UITableViewController {
var posts = [Post]()
var databaseRef: FIRDatabaseReference!
var storageRef: FIRStorageReference!

override func viewDidLoad() {
    super.viewDidLoad()
    databaseRef = FIRDatabase.database().reference()
    storageRef = FIRStorage.storage().reference()
    let userPostRef = self.databaseRef.child("posts")
    userPostRef.queryOrderedByChild("time").observeEventType(.ChildAdded, withBlock: {(snapshot) in
        if let postAdd  = snapshot.value as? NSDictionary{

            let url = snapshot.value?["postPhoto"] as! String
            let userPhotoUrl = snapshot.value?["userPhoto"] as! String
            let myPost = Post(data: postAdd)

            FIRStorage.storage().referenceForURL(url).dataWithMaxSize(10 * 1024 * 1024, completion: { (data, error) in
                let postPhoto = UIImage(data: data!)

                                    })
            FIRStorage.storage().referenceForURL(userPhotoUrl).dataWithMaxSize(10 * 1024 * 1024, completion: { (data, error) in
                let userPhoto = UIImage(data: data!)
                                })

            self.posts.insert(myPost, atIndex: 0)
            self.tableView.reloadData()

        }




})

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

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return posts.count


}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "postCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)as! timelineTableViewCell
    //Dispatch the main thread here
    dispatch_async(dispatch_get_main_queue()) {
        cell.usernameLabel.text = self.posts[indexPath.row].username
        cell.postText.text = self.posts[indexPath.row].postText
        cell.timeLabel.text = self.posts[indexPath.row].time
        cell.postPhoto.image = self.posts[indexPath.row].postPhoto
        cell.userPhoto.image = self.posts[indexPath.row].userPhoto


    }
    return cell
   }
   }

我的Post结构希望对您有所帮助!

struct  Post {
var username: String?
var postText: String?
var time: String?
var userPhoto: UIImage?
var postPhoto: UIImage?
var url: String?
var userPhotoUrl:String?
init(data: NSDictionary) {
   username = data["username"] as? String
   postText = data["postText"] as? String
    time = data["time"]as? String
    userPhoto = data["userPhoto"] as? UIImage
    postPhoto = data["postPhoto"] as? UIImage
    url = data["postPhoto"] as? String
    userPhotoUrl = data["userPhoto"] as? String
}

}

希望能得到一些建议!


问题答案:

只需更改以下方法

func downloadPost(){
    let userPostRef = self.databaseRef.child("posts")

    userPostRef.queryOrderedByChild("time").observeEventType(.ChildAdded, withBlock: {(snapshot) in
        if let postAdd  = snapshot.value as? NSDictionary {
            print("\(snapshot.value)")
            let url = snapshot.value?["postPhoto"] as! String
            let userPhotoUrl = snapshot.value?["userPhoto"] as! String

            var myPost = Post(data: postAdd) //variable change to "var" because going to modify

            let url2 = NSURL(string: url)  //postPhoto URL
            let data = NSData(contentsOfURL: url2!) // this URL convert into Data
            if data != nil {  //Some time Data value will be nil so we need to validate such things 
             myPost.postPhoto = UIImage(data: data!)
            }


            let url3 = NSURL(string: userPhotoUrl)  //userPhoto URL
            let data2 = NSData(contentsOfURL: url3!)  //Convert into data
            if data2 != nil  {  //check the data
            myPost.userPhoto = UIImage(data: data2!)  //store in image
            }




            self.posts.insert(myPost, atIndex: 0)  // then add the "myPost"  Variable
            print(self.posts.count)
        }
        self.tableView.reloadData()  // finally going to load the collection of data in tableview
    })


}

它需要大量的加载时间。因为转换图像并将其存储在主队列中。

另一种方法是存储URL并在表视图显示中访问URL。可以在imageview中应用异步加载。



 类似资料:
  • 我无法检索Flutter Web上Firebase存储的图像。 附有:

  • Firebase存储图像三分之二天未显示。我有一些发布的应用程序,在这些应用程序里面有一些图片显示从Firebase存储,现在突然它不显示/查看。我还从浏览器>Firebase Console>Storage检查了那些图像,但它没有显示图像预览,并且显示:“错误加载预览”。但我的Firebase Firestore数据显示没有任何错误。我查了储存配额,没有问题。我的火力点计划是“燃烧”,随走随付。

  • 我存储我的图像在Firebase存储现在我想显示在我的页面上 我的设置就是这样的。 这是我的get方法。 我只是尝试了一下,因为它是写在谷歌云计算的npm文档上的。我试着把它放在get方法中,看看它是如何工作的。 之后,我得到了这个错误... 这是我的ajaxget方法 这里是否有人可以指导我如何从firebase存储中检索图像并将其显示在我的网页上?。使用谷歌云npm?因为我读到一些线程,节点j

  • 我已经上传了一些图像到Firebase的存储,然后我需要在一个网站上显示所有这些图像。我已经阅读了firebase的文档,但仍然无法找到正确的方式来显示所有的图片。所以,我的问题是,我的代码在什么地方错误地显示了一个偶数的单个图像?如何在web上同时显示所有图像(我读过一篇关于堆栈溢出的帖子,它可能需要获取所有图像的URL,所以,子问题是如何获取所有图像的URL)?顺便说一下,我的Firebase

  • 我正在尝试从Firebase中检索一个Base64编码的图像,并在Android Studio开发的应用程序上显示它。代码正在ImageView元素上放一些东西,但我认为那只是一个白色图像,因为ImageView元素的背景刚好消失了。有人能帮忙吗?

  • 问题内容: 如何在SQLite中存储和获取图像以及以什么格式保存图像?如果用一个例子来解释会更有用。 问题答案: 图像本身无法存储到数据库列中,但是您可以先将其转换为字符串,然后再存储。该字符串称为base64字符串。据我所知,任何图像都可以转换为相反的图像。 编码为基数64: 现在,您的UIImage对象将转换为String!将strBase64保存到SQLite DB。记住要用作列类型,因为此