我正在尝试从Firebase调用将图像视图更改为数据库中存储的数据的数据。调用代码时,我看到
let image: UIImageView = {
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String : Any] else {
return }
let user = MyUser(dictionary: dictionary as [String : AnyObject])
let profileImageView = UIImageView()
profileImageView.translatesAutoresizingMaskIntoConstraints = false
profileImageView.contentMode = .scaleAspectFill
profileImageView.layer.cornerRadius = 30
profileImageView.clipsToBounds = true
profileImageView.image = UIImage(named: "app")
profileImageView.image=#imageLiteral(resourceName: "user")
profileImageView.loadImageUsingCacheWithUrlString(user.profileImageUrl!)
return profileImageView
}, withCancel: { (err) in
print("attempting to load information")
})
// return profileImageView
}()
注释掉的返回函数在哪里,我得到了错误:使用未解决的标识符'profileImageView';您是说'provideImageData'吗? 为什么?我假设由于返回值位于闭包内,因此它将知道profileImageView是什么。
答案 0 :(得分:1)
分隔图像视图和实时数据库侦听器:
let imageView: UIImageView {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
imageView.layer.cornerRadius = 30
imageView.clipsToBounds = true
return imageView
}
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String : Any] else {
return }
let user = MyUser(dictionary: dictionary as [String : AnyObject])
// Set the image view's image
imageView.image = // ...
}, withCancel: { (err) in
print("attempting to load information")
})