Firebase图片对象不存在?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Firebase图片对象不存在?相关的知识,希望对你有一定的参考价值。
我目前正在学习如何向/从Firebase上传/下载图像。但是,我遇到了两个问题:
- 当我将图像上传到我的Firebase控制台时,它说它的类型是“application / octet-stream”,这不是我想要的(我上传了一个jpeg文件)
这是代码:
@IBAction func completeSignUpButton(_ sender: UIButton) {
let userProfileImageRef = storage.child("userProfileImage")
//UPloading the photo into firebase
let data = Data()
//uploading user profile picture
if self.userInitialPhoto.image != nil {
if let uploadData = UIImagePNGRepresentation(self.userInitialPhoto.image!) {
userProfileImageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
if error != nil {
print(error?.localizedDescription)
return
} else {
print("Image upload success!")
//self.dismiss(animated: true, completion: nil)
self.performSegue(withIdentifier: "completeRegistrationSegue", sender: self)
}
})
}
} else {
print("You need to pick a picture!")
}
}
所以这可能是导致我的第二个问题的问题:
- 当我尝试在我的应用中将图像从Firebase加载到我的UIImage时,它表示不存在Object Optional(“userID ”)/ userProfileImage。
这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
docRef = Firestore.firestore().collection("user Status").document("Current Status")
self.userStatusTableView.reloadData()
let referencePath = Storage.storage().reference(withPath: "(userID)/userProfileImage")
let userProfileImageRef = Storage.storage().reference().child("(userID)/userProfileImage")
userProfileImageRef.getData(maxSize: 1 * 1024 * 1024) { (data, error) in
if error != nil {
print("There's an eror downloading the image(error?.localizedDescription)")
} else {
self.userProfileImage.image = UIImage(data: data!)
}
}
}
我想知道我应该怎么做才能解决这些问题。
感谢您的帮助,祝大家新年快乐。
答案
将图像上传到firebase时,您将元数据传递为nil。你需要在那里传递图像元数据
let metaData = FIRStorageMetadata()
metaData.contentType = "image/jpg"
并在上传complitionBlock中,您可以使用获取该图像的URL
if error == nil , let meta = meta {
if let url = meta.downloadURL()?.absoluteString {
//use this url to access your image
}
}
以上是关于Firebase图片对象不存在?的主要内容,如果未能解决你的问题,请参考以下文章
我可以在 android studio 的片段上使用 Firebase 吗?
Firebase 存储上传错误:“对象 albumNum/1 不存在。”