【问题标题】:user images not loading from Firebase storage未从 Firebase 存储加载的用户图像
【发布时间】:2017-06-15 13:15:20
【问题描述】:

运行此代码时,我只从资产返回UIImage(named: "Home Button"),而不是每个用户选择的图片?任何想法为什么??

class usersScreenVC: UITableViewController {

let cellId = "cellId"

var users = [User]()

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(handleCancel))

    tableView.register(UserCell.self, forCellReuseIdentifier: cellId)

    fetchUser()
}

func handleCancel() {
    self.dismiss(animated: true, completion: nil)
}

func fetchUser() {
    FIRDatabase.database().reference().child("Users").observe(.childAdded, with: { (snapshot) in

        if let dictionary = snapshot.value as? [String: AnyObject] {
            let user = User()

            self.users.append(user)

            user.DisplayName =  dictionary["Display Name"] as? String
            user.SubtitleStatus = dictionary["SubtitleStatus"] as? String

            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
    }, withCancel: nil)
}

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

}

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


           let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)

    let user = users[indexPath.row]
    cell.textLabel?.text = user.DisplayName
    cell.detailTextLabel?.text = user.SubtitleStatus

    cell.imageView?.image = UIImage(named: "Home Button")

    if let profileImageURL = user.profileImageURL{
        let url = URL(string: profileImageURL)


       URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in     
            //this mean download hit an error so lets return out.
            if error != nil {
                print(error!)
                return
            }

            DispatchQueue.main.async(execute: {     
                cell.imageView?.image = UIImage(data: data!)
            })      
        }).resume()
    }

    return cell
}

class UserCell: UITableViewCell {

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}
}//class

【问题讨论】:

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


    【解决方案1】:

    我认为您的代码有两个问题可能会有所帮助。首先,从 Firebase 存储加载图像时使用 SD WebImage 被认为是最佳做法。 SD WebImage会处理异步加载图片,缓存图片,保证同一个URL不会被多次下载,虚假URL不会被重试。 SD WebImage 随 Firebase 一起提供,因此您需要做的就是确保已将 Storage 添加到 PodFile 并在 TableViewController 中为 SDWebImage 和 FirebaseStorage 创建导入。然后你应该把你的 cellForRowAt indexPath 修改成这样:

        import SDWebImage
        import FirebaseStorage 
    
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    
           let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)
    
           let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
    
            let user = users[indexPath.row]
            cell.textLabel?.text = user.DisplayName
            cell.detailTextLabel?.text = user.SubtitleStatus
    
            cell.imageView?.image = UIImage(named: "Home Button")
    
            if let profileImageURL = user.profileImageURL{
            let url = URL(string: profileImageURL)
    
            cell.imageView?.sd_cancelCurrentImageLoad()
    
            cell.imageView?.sd_setImage(with: url, completed: { (image, error, type, url) in
            DispatchQueue.main.async {
            cell.layoutSubviews()
            }
        })      
    
    }
    
    return cell
    }
    

    其次,您在哪里为每个用户设置个人资料图片?我可以看到您设置显示名称和字幕状态,但我看不到您在哪里添加个人资料图像。也许您打算执行以下操作:

        if let dictionary = snapshot.value as? [String: AnyObject] {
            let user = User()
    
            self.users.append(user)
    
            user.DisplayName =  dictionary["Display Name"] as? String
            user.SubtitleStatus = dictionary["SubtitleStatus"] as? String
    
            //did you forget to add the profile image url like this?
           user.profileImageURL = dictionary["ProfileImage"] as? String
    
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
    }, withCancel: nil)
    

    【讨论】:

      猜你喜欢
      • 2020-09-02
      • 2020-04-21
      • 2020-06-06
      • 2016-12-28
      • 2019-09-22
      • 2023-03-22
      • 1970-01-01
      • 2019-10-06
      • 2020-06-28
      相关资源
      最近更新 更多