用户图片未从Firebase存储加载

时间:2017-06-15 13:15:20

标签: ios swift firebase firebase-realtime-database firebase-storage

运行此代码时,我只能从assests而不是每个用户选择的图片中返回UIImage(named: "Home Button")?任何想法为什么??

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

1 个答案:

答案 0 :(得分:0)

我认为您的代码有两个问题可能会有所帮助。首先,从Firebase存储加载图像时,最好使用SD WebImage。 SD WebImage将处理异步加载图像,缓存图像,保证不会多次下载相同的URL,并且不会重试伪造的URL。 SD WebImage附带Firebase,因此您需要做的就是确保已将存储添加到PodFile并在TableViewController中为SDWebImage和FirebaseStorage创建导入。然后你应该将你的cellForRowAt indexPath修改为这样的东西:

    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)

其次,您在哪里为每个用户设置个人资料图片?我可以看到您设置显示名称和字幕状态,但我看不到您要添加配置文件图像的位置。也许你打算做以下事情:

{{1}}