我正在将用户图像上传到Firebase存储中。但是,当上传图片时,我无法在Firebase存储控制台中查看图片。该文件仅在下载图像时显示。
Firebase控制台中未显示图片:
我如何重组我的代码以解决此问题?
let StorageRef = Storage.storage().reference(forURL: "gs://tunnel-vision-458d6.appspot.com/").child("profileImage.jpg").child(user.uid)
if let profileImge = self.selectedImage, let imageData = profileImge.jpegData(compressionQuality: 0.1) {
StorageRef.putData(imageData, metadata: nil) { (metadata, error) in
if error != nil {
return
StorageRef.downloadURL { url, error in //note the change here to storageRef
if error != nil { //handle the error if there is one
print("could not access the downloadURL")
return
} else {
let LocalFile = URL(string: "gs://tunnel-vision-458d6.appspot.com/")
let riversRef = StorageRef.child("profileImage.jpg")
let uploadTask = riversRef.putFile(from: LocalFile!, metadata: nil) { metadata, error in
guard let metadata = metadata else {
return
}
riversRef.downloadURL { (url, error) in
guard url != nil else {
return
let ref = Database.database().reference()
let usersReference = ref.child("users")
// print(usersReference.description()) : https://tvmm-de1ba.firebaseio.com/users
let uid = user.uid
let newUsersReference = usersReference.child(uid)
newUsersReference.setValue(["username": self.UsernameTextField.text!, "email" : self.EmailTextField.text!, "ProfileImageUrl": riversRef])
print("\(user.email!) created")
}
}
}
}
[Firebase Storage无法将上传的数据识别为图像,您可以通过将Type
设置为application/octet-stream
(基本上是一堆字节)来看到它。
[要解决此问题,您需要pass in image metadata您上传的文件/图像类型。从该文档中:
// Create storage reference let mountainsRef = storageRef.child("images/mountains.jpg") // Create file metadata including the content type let metadata = StorageMetadata() metadata.contentType = "image/jpeg" // Upload data and metadata mountainsRef.putData(data, metadata: metadata)