“StorageMetadata”类型的值没有成员“downloadURL”

时间:2018-05-21 11:57:48

标签: swift firebase firebase-storage

嘿,我需要帮助这个编码,我不知道发生了什么。它前几天正在工作。错误在if let ur; = metaData?.downladURL()

之后出现
func uploadProfileImage(_ image:UIImage, completion: @escaping ((_ url:URL?)->())) {
        guard let uid = Auth.auth().currentUser?.uid else { return }
        let storageRef = Storage.storage().reference().child("user/\(uid)")

        guard let imageData = UIImageJPEGRepresentation(image, 0.75) else { return }

        let metaData = StorageMetadata()
        metaData.contentType = "image/jpg"

        storageRef.putData(imageData, metadata: metaData) { metaData, error in
            if error == nil, metaData != nil {
                if let url = metaData?.downloadURL() {
                    completion(url)
                } else {
                    completion(nil)
                }
                // success!
            } else {
                // failed
                completion(nil)
            }
        }
    }

5 个答案:

答案 0 :(得分:18)

您可能已更新了Firebase广告连播。在Firebase 5.0中,他们摆脱了metaData?.downladURL()功能。您必须在其网站上关注更新的文档。从那里复制:

// Data in memory
let data = Data()

// Create a reference to the file you want to upload
let riversRef = storageRef.child("images/rivers.jpg")

// Upload the file to the path "images/rivers.jpg"
let uploadTask = riversRef.putData(data, metadata: nil) { (metadata, error) in
  guard let metadata = metadata else {
    // Uh-oh, an error occurred!
    return
  }
  // Metadata contains file metadata such as size, content-type.
  let size = metadata.size
  // You can also access to download URL after upload.
  storageRef.downloadURL { (url, error) in
    guard let downloadURL = url else {
      // Uh-oh, an error occurred!
      return
    }
  }
}

答案 1 :(得分:3)

更新您的广告连播,然后将其放进去:

func uploadProfileImage(_ image:UIImage, completion: @escaping ((_ url:URL?)->())) {
    guard let uid = Auth.auth().currentUser?.uid else { return }
    let storageRef = Storage.storage().reference().child("user/\(uid)")

    guard let imageData = UIImageJPEGRepresentation(image, 0.75) else { return }

    let metaData = StorageMetadata()
    metaData.contentType = "image/jpg"

    storageRef.putData(imageData, metadata: metaData) { metaData, error in
        if error == nil, metaData != nil {

            storageRef.downloadURL { url, error in
                completion(url)
                // success!
            }
            } else {
                // failed
                completion(nil)
            }
        }
    }

答案 2 :(得分:2)

    // MARK: - handleRegister
func handleRegister() {
    guard let email = emailTextField.text, let password = passwordTextField.text, let name = nameTextField.text else {
        print("Error")
        return
    }

    Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
        if error != nil {
            // print("Error")
            return
        }

        // MARK: - hadleLogin// 

        guard let uid = Auth.auth().currentUser?.uid  else {
            return
        }

        // MARK: - image successful authenficated user

        let imageName = NSUUID().uuidString
        let storageRef = Storage.storage().reference().child("profile_images").child("\(imageName).png")

        if let profileImageUrl = self.profileImageView.image, let  uploadData = UIImageJPEGRepresentation(self.profileImageView.image!, 0.1) {
            storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in

                if error != nil, metadata != nil {
                    print(error ?? "")
                    return

                }

                storageRef.downloadURL(completion: { (url, error) in
                    if error != nil {
                        print(error!.localizedDescription)
                        return
                    }
                    if let profileImageUrl = url?.absoluteString {
                        let values = ["name": name, "email": email, "profileImageUrl": profileImageUrl]
                        self.registeUserIntoDatabaseWithUID(uid: uid, values: values as [String : AnyObject])
                    }
                })
            })
        }
    }
}

答案 3 :(得分:0)

从pod文件夹中

删除 FirebaseStorage。
 关闭Xcode。
 转到podFile,将 pod'Firebase / Storage'更改为 pod Firebase / Storage','〜> 4.0'
保存 podFile并打开终端
输入: pod更新命中输入
打开Xcode并编写以下内容:

if let profileImg     = self.selectedImage, let photoData = profileImg.jpegData(compressionQuality:0.1) {
                storageRef.putData(photoData, metadata: nil, completion: { (metadata, error) in
                    if error != nil  {
                        return
                    }

                   let profileImageUrl = metadata?.downloadURL()?.absoluteString
                    let ref                 = Database.database().reference()
                    let usersReference      = ref.child("user")

                    let newUserReference    = usersReference.child(uid!)
                    newUserReference.setValue(["username" :self.usernameTextField.text!, "email" :self.emailTextField.text!, "ProfileImageUrl":profileImageUrl])
                })

运行项目,错误将消失

答案 4 :(得分:-1)

guard let uid = Auth.auth().currentUser?.uid else { return }

let storageRef = Storage.storage().child("user/\(uid)")

storageRef.downloadURL