简体   繁体   English

对于使用firebase登录的特定用户,如何使用swift保存用户的个人资料图片并加载?

[英]How do i save a user 's profile picture with swift and load it, for the specific user logged in using firebase?

I have a basic user creation activity, name, email and password. 我有一个基本的用户创建活动,名称,电子邮件和密码。 I am trying to save the user's profile picture and load it in their profile page once they login. 我正在尝试保存用户的个人资料图片,并在登录后将其加载到个人资料页面中。 How do I do that? 我怎么做?

I already stored the user's name and email in my database, but I am not sure how to save their profile picture. 我已经将用户的姓名和电子邮件存储在我的数据库中,但我不确定如何保存他们的个人资料图片。 I tried searching the problem on stack overflow but most solution I found were too old. 我尝试在堆栈溢出时搜索问题,但我找到的大多数解决方案都太旧了。

func saveUserProfile(){
    let userRefrence = Database.database().reference().child("Users").childByAutoId()

    let userDictionary:[String:String] = ["name":users.name, "email" :users.userEmail]
    self.userRefrence.setValue(userDictionary)
}

If you're already using Firebase than first you'd want to save the image using Firebase Storage. 如果您已经使用Firebase,那么首先您需要使用Firebase存储来保存图像。

Here we grab a reference to storage and convert our image into data. 在这里,我们获取对存储的引用并将我们的图像转换为数据。

let fileName = NSUUID().uuidString
let profileImagesRef = Storage.storage().reference().child("profile_images/\(fileName)")

guard let uploadData = UIImageJPEGRepresentation(image, 0.3) else {
    return
}

Then we actually save our data into Firebase Storage 然后我们实际将数据保存到Firebase存储中

profileImagesRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
    if let error = error {
        return
    }

    // After the image has been successfully uploaded we need to grab its download url

    profileImagesRef.downloadURL(completion: { (downloadURL, error) in
        if let error = error {
            return
        }

        guard let downloadURL = downloadURL else {
            return
        }

        print("successfully fetched download URL: \(downloadURL.absoluteString)")
    })
})

You want to save the downloadUrl to your user model and use that to download your profile image when loading the profile. 您希望将downloadUrl保存到您的用户模型,并在加载配置文件时使用它来下载您的配置文件图像。

You can read about it in the Firebase documentation. 您可以在Firebase文档中阅读相关内容。

https://firebase.google.com/docs/storage/ios/download-files https://firebase.google.com/docs/storage/ios/download-files

https://firebase.google.com/docs/storage/ios/upload-files https://firebase.google.com/docs/storage/ios/upload-files

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Firebase 上保存与用户关联的个人资料图片 - Save profile picture associated with the user on Firebase Facebook iOS SDK 和 swift:如何获取用户的个人资料图片 - Facebook iOS SDK and swift: how get user's profile picture 如何更快地加载用户的个人资料照片(Firebase,迅速) - How to make faster the loading of the user's profile photo (Firebase, swift) 如何修复在用户配置文件下发送到Firebase数据库的用户输入? - How do I fix for the user inputs to be sent to Firebase database under the User's profile? 如何使用Swift在Firebase中更新用户的分数? - How can I update user's score in Firebase using Swift? 如何使用NSURLSession保存Facebook个人资料图片? - How do I save a facebook profile picture using NSURLSession? 如何在mysql中存储用户的个人资料图片 - How to store user's profile picture in mysql iOS-Swift:在UITableView中展开用户个人资料图片 - iOS - Swift : Expand user profile picture in UITableView 在 Swift 中使用 Parse 创建带有个人资料图片的用户 - Create a User with Profile Picture with Parse in Swift 如何快速从Firebase中为用户获取特定数据? - how can i get a specific data for a user from firebase in swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM