当我的用户注册后,他所有的用户信息都直接进入Firebase存储和数据库(例如:用户名和个人资料图片)。当我的用户注册时,用户名出现在他的个人资料上,但没有个人资料图像出现,如何解决此代码以更改此代码? 我需要用户注册时使用的个人资料图像在其个人资料屏幕上弹出
import Firebase
class InteractivViewController: UIViewController {
@IBOutlet weak var UsernameText: UILabel!
@IBOutlet weak var ProfileImage: UIImageView!
@IBOutlet weak var DisplayText: UILabel!
var DatabaseRef: DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
DatabaseRef = Database.database().reference()
if let userID = Auth.auth().currentUser?.uid {
DatabaseRef.child("users").child(userID).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let dictionary = snapshot.value as? NSDictionary
let username = dictionary?["username"] as? String ?? "username"
if let profileImageUrl = dictionary?["photo"] as? String {
func getProfileImage(uid: String, success: @escaping (UIImage?)->Void)
{
let storageRef = Storage.storage().reference().child("usersImages")
storageRef.child("\(uid).jpg").getData(maxSize: 1024*1024) { (data, error) in
guard let data = data else {return}
DispatchQueue.main.async {
success(UIImage(data: data))
getProfileImage(uid: userID) { image in
self.ProfileImage.image = image
}
}
}
}
func upload(image: UIImage, to firebasePath: String, completion: @escaping (String?, Error?) -> Void) {
//... creates data from the image...
var data = NSData()
//Compress the image by any percent
data = image.jpegData(compressionQuality: 0.8)! as NSData
//...sets the upload path
let filePath = "\(Auth.auth().currentUser!.uid)0)" // path where you wanted to store img in storage
print(filePath)
let metaData = StorageMetadata()
metaData.contentType = "image/jpg"
let storageRef = Storage.storage().reference().child(filePath)
storageRef.putData(data as Data, metadata: metaData){(metaData,error) in
if let error = error {
print(error.localizedDescription)
completion(nil, error)
return
} else {
storageRef.downloadURL(completion: { (url, error) in
//Returns the url string to the newly uploaded image so that we can set the user's photoURL database property to this string
Database.database().reference().child(firebasePath).setValue(url!.absoluteString)
completion(url!.absoluteString, nil)
})
}
}
let image = UIImage(named: "someImage")
upload(image: image!, to: "Users/\(Auth.auth().currentUser!.uid)/photoUrls/0", completion: { urlString, error in
Database.database().reference().child("Users/\(Auth.auth().currentUser!.uid)/photoUrls/0").setValue(urlString)
},
Database.database().reference().child("Users/\(Auth.auth().currentUser!.uid)/photoUrls/0").observeSingleEvent(of: .value) { (snapshot) in
let urlString = snapshot.value as! String
let task = URLSession.shared.dataTask(with: URL(string: urlString)!) {(data, response, error) in
if let image: UIImage = UIImage(data: data!) {
DispatchQueue.main.async {
//update the image
self.ProfileImage.image = image
}
}
}
task.resume()
}
)}
}
self.UsernameText.text = username
self.DisplayText.text = username
return
}
答案 0 :(得分:0)
您必须使用从firebase检索到的网址,并使用其存储API下载
let storage = Storage.storage()
let storageRef = storage.reference()
let ref = storage.reference(forURL: profileImageUrl)
ref.getData(maxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
// Uh-oh, an error occurred!
} else {
self.ProfileImage = UIImage(data: data!)
self.view.addSubview(self.ProfileImage)
self.reloadInputViews()
}
}
答案 1 :(得分:0)
您需要将指向引用的URL存储在数据库中用户对象内的存储桶中。我使用的功能是使用UIImage
和String
表示Firebase路径并返回上传图像的URL:
func upload(image: UIImage, to firebasePath: String, completion: @escaping (String?, Error?) -> Void) {
//... creates data from the image...
var data = NSData()
//Compress the image by any percent
data = UIImageJPEGRepresentation(image, 0.8)! as NSData
//...sets the upload path
let filePath = "\(Auth.auth().currentUser!.uid)0)" // path where you wanted to store img in storage
print(filePath)
let metaData = StorageMetadata()
metaData.contentType = "image/jpg"
let storageRef = Storage.storage().reference().child(filePath)
storageRef.putData(data as Data, metadata: metaData){(metaData,error) in
if let error = error {
print(error.localizedDescription)
completion(nil, error)
return
} else {
storageRef.downloadURL(completion: { (url, error) in
//Returns the url string to the newly uploaded image so that we can set the user's photoURL database property to this string
Database.database().reference().child(firebasePath).setValue(url!.absoluteString)
completion(url!.absoluteString, nil)
})
}
}
}
要上传图像并更新数据库中的用户对象,可以执行以下操作:
let image = UIImage(named: "someImage")
upload(image: image, to: "Users/\(Auth.auth().currentUser!.uid)/photoUrls/0", completion: { urlString, error in
Database.database().reference().child("Users/\(Auth.auth().currentUser!.uid)/photoUrls/0").setValue(urlString)
}
然后,您可以通过以下方式获取图像:
Database.database().reference().child("Users/\(Auth.auth().currentUser!.uid)/photoUrls/0").observeSingleEvent(of: .value) { (snapshot) in
let urlString = snapshot.value as! String
let task = URLSession.shared.dataTask(with: URL(string: urlString)!) {(data, response, error) in
if let image: UIImage = UIImage(data: data!) {
DispatchQueue.main.async {
//update the image
self.ProfileImage.image = image
}
}
}
task.resume()
}