我有一个类似Instagram的应用程序,主要供稿中的图像帖子上方有一个小的用户个人资料图片。
我已经弄清楚如何获取当前用户的用户个人资料图片,但我无法弄清楚如何获取添加帖子的人的用户个人资料图片。请参阅下面的代码。
如何获取创建帖子的用户图片?
我的Firebase数据库结构是: [我的应用]> “帖子”>帖子ID> “description”,“image”,“title”,“uid”,“userName”,“location”
我的Firebase存储结构是: [我的应用]> “用户”> uid> “profile_pic.jpg”
我使用的是Swift 3和Firebase。
感谢您的任何建议!!
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
}
}