简体   繁体   English

如何从 firebase 存储中检索数据

[英]How to retrieve data from firebase storage

Hey I am trying to retrieve the user image saved in the firebase storage but I am using this storage path (the folder of profile pictures in firebase) as a string(child path) to update the user image.嘿,我正在尝试检索保存在 firebase 存储中的用户图像,但我正在使用此存储路径(firebase 中的个人资料图片文件夹)作为字符串(子路径)来更新用户图像。 When the user profile updates there are no errors this code just didn't update the user image as I expected what am I missing here that will allow me to successfully my user image and solve my problem?当用户配置文件更新没有错误时,这段代码只是没有像我预期的那样更新用户图像,我在这里遗漏了什么可以让我成功地创建用户图像并解决我的问题?

                            var spaceRef = Storage.storage().reference()
                        let storagePath = "gs://tunnel-vision-bc055.appspot.com/images/space.jpg"
                        spaceRef = Storage.storage().reference(forURL: storagePath)
                       let reference = spaceRef.child("profilePic")

                        // UIImageView in your ViewController
                        let imageView: UIImageView = self.ProfileImage

                        // Placeholder image
                        let placeholderImage = UIImage(named: "image/jpeg")

                        // Load the image using SDWebImage
                        imageView.sd_setImage(with: reference, placeholderImage: placeholderImage)
                            // Uh-oh, an error occurred!
                            return
                          }

This will not be the answer to all your issues, but hopefully some of them.这不会是您所有问题的答案,但希望是其中的一些问题。

First of all, in Swift, variables and constants starts with a lowercase letter.首先,在Swift中,变量和常量以小写字母开头。 Classes with uppercase.大写字母类。 For example, your ProfileImage should be profileImage.例如,您的 ProfileImage 应该是 profileImage。 For clarity, when I type profileImage I refer to your ProfileImage.为清楚起见,当我键入 profileImage 时,我指的是您的 ProfileImage。

Now, types.现在,类型。 You have a constant profileImage in the code you posted.您发布的代码中有一个不变的 profileImage 。 This is of type String.这是字符串类型。 You set the value of the key "ProfileImageUrl" to it.您将键“ProfileImageUrl”的值设置为它。 I would change it's name to profileImageURL.我会将其名称更改为 profileImageURL。

Then (according to one of your comments) you are calling self.profileImage然后(根据您的评论之一)您正在调用 self.profileImage

let imageView: UIImageView = self.profileImage

Since you're not seeing any errors I assume you also have a class property called profileImage of type UIImageView.由于您没有看到任何错误,我假设您还有一个名为 profileImage 的 class 属性,类型为 UIImageView。

The line线

// let user = User(ProfileImage: ProfileImage)

will not help you.不会帮助你。 As far as I understand from the code you posted - and what you are trying to achieve - you may as well delete it.据我从您发布的代码中了解到 - 以及您想要实现的目标 - 您也可以删除它。 Or comment it (//) as I did above.或者像我上面那样评论它(//)。

What you want to do is to set an imageView's image.你想要做的是设置一个 imageView 的图像。

For this you need to do为此你需要做

imageView.image = image

And to be able to do that you need an imageView and an image.为此,您需要一个 imageView 和一张图片。 If you don't know how to create an imageView, search for answers here on SO.如果您不知道如何创建 imageView,请在此处搜索答案。

You can then create the image from the url you get from Firebase and set it to the imageView. Kind of like this然后,您可以根据从 Firebase 获得的 url 创建图像,并将其设置为 imageView。有点像这样

let profileImageURL = value?["ProfileImageUrl"] as? String ?? ""
let url = URL(string: profileImageURL)

DispatchQueue.global().async {
    guard let data = Data(contentsOf: url) else {return} // if url is nil, return block is executed and code below is not
    DispatchQueue.main.async {
        self.imageView.image = UIImage(data: data)
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM