Swift 如何从发布新帖子的用户的Firebase存储中获取配置文件图像?
我有一个类似instagram的应用程序,在主提要中他们的图片帖子上方有一个小的用户档案图片 我已经找到了如何获取当前用户的用户配置文件映像,但是我无法找到如何获取添加帖子的人的用户配置文件映像。请看下面我的代码 如何获取创建帖子的用户图像 我的Firebase数据库结构是: [我的应用]>“帖子”>帖子id>“描述”、“图片”、“标题”、“uid”、“用户名”、“位置” 我的Firebase存储结构是: [我的应用]>“用户”>uid>“个人资料”\u pic.jpg” 我正在使用Swift 3和Firebase 谢谢你的建议Swift 如何从发布新帖子的用户的Firebase存储中获取配置文件图像?,swift,firebase,firebase-realtime-database,tableview,firebase-storage,Swift,Firebase,Firebase Realtime Database,Tableview,Firebase Storage,我有一个类似instagram的应用程序,在主提要中他们的图片帖子上方有一个小的用户档案图片 我已经找到了如何获取当前用户的用户配置文件映像,但是我无法找到如何获取添加帖子的人的用户配置文件映像。请看下面我的代码 如何获取创建帖子的用户图像 我的Firebase数据库结构是: [我的应用]>“帖子”>帖子id>“描述”、“图片”、“标题”、“uid”、“用户名”、“位置” 我的Firebase存储结构是: [我的应用]>“用户”>uid>“个人资料”\u pic.jpg” 我正在使用Swift
var eventPosts = NSMutableArray()
@IBOutlet weak var eventPostsTableView: UITableView!
@IBAction func logoTapped(_ sender: Any) {
}
func loadData() {
FIRDatabase.database().reference().child("events").observeSingleEvent(of: .value, with: { (snapshot) in
if let eventPostsDictionary = snapshot.value as? [String: AnyObject] {
for post in eventPostsDictionary {
self.eventPosts.add(post.value)
}
self.eventPostsTableView.reloadData()
}
})
}
override func viewDidLoad() {
super.viewDidLoad()
self.eventPostsTableView.delegate = self
self.eventPostsTableView.dataSource = self
loadData()
// Do any additional setup after loading the view, typically from a nib.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.eventPosts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! EventTableViewCell
// Configure the cell
let event = self.eventPosts[indexPath.row] as! [String: AnyObject]
cell.titleLabel.text = event["title"] as? String
cell.userNameLabel.text = event["userName"] as? String
if ("image" as? String) != nil {
let user = FIRAuth.auth()?.currentUser
let storage = FIRStorage.storage()
let storageRef = storage.reference()
let profilePicRef = storageRef.child("Users").child((user?.uid)!+"/profile_pic.jpg")
profilePicRef.data(withMaxSize: 1 * 1024 * 1024, completion: { (data, error) -> Void in
if (error != nil) {
print("Unable to download image")
} else {
if(data != nil)
{
print("User already has image")
cell.userProfileImageView.image = UIImage(data: data!)
}
}
})
}
if let imageFileName = event["image"] as? String {
let imageRef = FIRStorage.storage().reference().child("images/\(imageFileName)")
imageRef.data(withMaxSize: 2 * 1024 * 1024) { (data, error) -> Void in
if error == nil {
// Success
let image = UIImage(data: data!)
cell.flyerImageView.image = image
cell.flyerImageView.clipsToBounds = true
} else {
// Error
print("Error downloading image: \(error?.localizedDescription)")
}
}
}
return cell
}
}
您可能需要将imageRef中的错误更改为类似err的内容,我已经看到了由此产生的问题,但让我来研究一下。感谢Nivix的建议!