【发布时间】:2018-08-29 17:25:24
【问题描述】:
目前,我设置了一个 imageview,它引用并显示了我存储在 assets 文件夹中的图像。如何让它从 Firebase 中提取用户图像?
lazy var profileImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "profileUpload")
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
//imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleSelectProfileImageView)))
imageView.isUserInteractionEnabled = true
return imageView
}()
此函数引用并拉取我需要呈现的 profileImageUrl。我怎样才能将它添加到我以前的惰性 var 中?
func fetchUser() {
Database.database().reference().child("users").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User()
user.profileImageUrl = dictionary["profileImageUrl"]as? String
}
}, withCancel: nil)
}
有没有办法通过将单元格替换为 imageView 来复制此方法?这似乎要容易得多,并且需要的代码要少得多。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCell
cell.textLabel?.textColor = UIColor.white
let user = users[indexPath.row]
cell.textLabel?.text = user.name
cell.detailTextLabel?.text = user.email
cell.detailTextLabel?.textColor = UIColor.white
cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 15.0)
if let profileImageUrl = user.profileImageUrl {
cell.profileImageView.loadImageUsingCacheWithUrlString(profileImageUrl)
}
return cell
}
上面的代码在表格视图中提取并呈现了我需要的图像。
【问题讨论】:
-
您是否尝试在浏览器中粘贴
firebase image url?它会显示图像吗?如果是,那么您不需要下载图像,而是使用 SDWebImage 库。
标签: swift xcode firebase firebase-realtime-database