【发布时间】:2018-06-11 19:25:02
【问题描述】:
我目前正在学习如何向 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!)
}
}
}
我想知道我应该如何解决这些问题。
提前感谢您的帮助,祝大家新年快乐。
【问题讨论】:
标签: ios firebase uiimage firebase-storage