2017-04-04 37 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

相關問題