2017-04-04 75 views
0

我试图显示当前用户发布的所有图像。正如您在表格视图中的图片中看到的,每次当前用户发布图像时,它都会自动为当前用户信息下的图像创建唯一的ID。如何通过其uid的Swift Firebase获取图片?

screenshot

在这个图像,你会看到,当用户上传的图像它创建一个名为节点为媒体与他们的孩子的形象。

screenshot

这里是我的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "CellMe", for: indexPath) as! MyPosttTableViewCell 


    FIRDatabase.database().reference().child("users").child(FIRAuth.auth()!.currentUser!.uid).observeSingleEvent(of: .value, with: { (snapshot) in 
    // check if user has photo 
    if snapshot.hasChild("media") != nil{ 
     print("found it") 

    //set image locatin 
    let filePath = "\(FIRAuth.auth()!.currentUser!.uid)/\("media")" 


    FIRStorage.storage().reference().child(filePath).data(withMaxSize: 10*1024*1024, completion: { (data, error) in 

    let userPhoto = UIImage(data: data!) 
    cell.Myimages.image = userPhoto 

    }) 

    } 
    else { 
     print("nil") 
     } 
     }) 

     return cell 


    }} 

回答

0

媒体在用户节点存储为一个的NSDictionary,而不是作为一个字符串。您将不得不遍历媒体节点以获取所有文件的路径。你可以做类似

//Create reference to media directly instead 
FIRDatabase.database().reference().child("users").child(FIRAuth.auth()!.currentUser!.uid).child("media").observeSingleEvent(of: .value, with: { (snapshot) in 
    if let media = snapshot.value as? NSDictionary { 

    //Loop thorugh all the entries in your media node to get all the media paths 
    for (_, value) in media { 
     FIRStorage.storage().reference().child("media").child(value as! String) 

     //get the file now that you have the reference 
    } 
    } 
}) 
+0

我想在for循环下写什么? cell.myimages.image =? – ASH