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

関連する問題