2017-06-15 51 views
0

運行此代碼時,我只會從資產返回UIImage(named: "Home Button")而不是每個用戶選擇的圖片?任何想法爲什麼?未從Firebase存儲中加載用戶圖片

class usersScreenVC: UITableViewController { 

let cellId = "cellId" 

var users = [User]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(handleCancel)) 

    tableView.register(UserCell.self, forCellReuseIdentifier: cellId) 

    fetchUser() 
} 

func handleCancel() { 
    self.dismiss(animated: true, completion: nil) 
} 

func fetchUser() { 
    FIRDatabase.database().reference().child("Users").observe(.childAdded, with: { (snapshot) in 

     if let dictionary = snapshot.value as? [String: AnyObject] { 
      let user = User() 

      self.users.append(user) 

      user.DisplayName = dictionary["Display Name"] as? String 
      user.SubtitleStatus = dictionary["SubtitleStatus"] as? String 

      DispatchQueue.main.async { 
       self.tableView.reloadData() 
      } 
     } 
    }, withCancel: nil) 
} 

override  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return users.count 

} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 


      let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId) 

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) 

    let user = users[indexPath.row] 
    cell.textLabel?.text = user.DisplayName 
    cell.detailTextLabel?.text = user.SubtitleStatus 

    cell.imageView?.image = UIImage(named: "Home Button") 

    if let profileImageURL = user.profileImageURL{ 
     let url = URL(string: profileImageURL) 


     URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in  
      //this mean download hit an error so lets return out. 
      if error != nil { 
       print(error!) 
       return 
      } 

      DispatchQueue.main.async(execute: {  
       cell.imageView?.image = UIImage(data: data!) 
      })  
     }).resume() 
    } 

    return cell 
} 

class UserCell: UITableViewCell { 

override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
    super.init(style: .subtitle, reuseIdentifier: reuseIdentifier) 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

} 
}//class 

回答

0

我認爲可能有幫助的代碼有兩個問題。首先,從Firebase存儲加載圖像時使用SD WebImage被認爲是最佳做法。 SD WebImage將處理異步加載圖像,緩存圖像,保證相同的URL不會被多次下載,並且不會重新嘗試僞造的URL。 SD WebImage隨Firebase一起提供,所以您只需確保已將存儲添加到您的PodFile中,並在您的TableViewController中爲SDWebImage和FirebaseStorage創建導入。那麼你應該修改你的cellForRowAt indexPath到這樣的事情:

import SDWebImage 
    import FirebaseStorage 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 


     let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId) 

     let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) 

     let user = users[indexPath.row] 
     cell.textLabel?.text = user.DisplayName 
     cell.detailTextLabel?.text = user.SubtitleStatus 

     cell.imageView?.image = UIImage(named: "Home Button") 

     if let profileImageURL = user.profileImageURL{ 
     let url = URL(string: profileImageURL) 

     cell.imageView?.sd_cancelCurrentImageLoad() 

     cell.imageView?.sd_setImage(with: url, completed: { (image, error, type, url) in 
     DispatchQueue.main.async { 
     cell.layoutSubviews() 
     } 
    })  

} 

return cell 
} 

所有的二,你在哪裏爲每個用戶設置個人資料圖片?我可以看到你設置顯示名稱和字幕狀態,但我看不到添加個人資料圖像的位置。也許你的意思是做如下事情:

if let dictionary = snapshot.value as? [String: AnyObject] { 
     let user = User() 

     self.users.append(user) 

     user.DisplayName = dictionary["Display Name"] as? String 
     user.SubtitleStatus = dictionary["SubtitleStatus"] as? String 

     //did you forget to add the profile image url like this? 
     user.profileImageURL = dictionary["ProfileImage"] as? String 

     DispatchQueue.main.async { 
      self.tableView.reloadData() 
     } 
    } 
}, withCancel: nil)