它将引发此错误:类型为“ StorageMetadata”的值没有成员“ downloadURL”

时间:2018-11-30 04:48:22

标签: ios swift firebase

我需要有关此编码的帮助,我不确定发生了什么。请不要重复此问题或类似问题,因为我已经在互联网上尝试了任何解决方案,但没有任何效果。我试图获得“上传后可以访问下载URL”。

我尝试过:我使用的是元数据的自动完成功能,它没有显示最接近我的是meta.path或metadata.storageReference的url选项。

第二次尝试:我知道API在5.0.0版本中已更改,因此我使用StorageReference的downloadURL方法。也会给我一个错误,也许是我把它放在错误的代码行上了。

这是课程:-

import UIKit
import Firebase
import FirebaseDatabase
import FirebaseStorage
import SwiftKeychainWrapper

class SignUpVC: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate{

@IBOutlet weak var userImagePicker: UIImageView!
@IBOutlet weak var userNameField: UITextField!
@IBOutlet weak var SignUpBtn: UIButton!

var userId: String!
var emailField: String!
var passwordField: String!
var imagePicker: UIImagePickerController!
var imageselected = false
var userName: String!

//Do any Additional SetUp after  loading the view
override func viewDidLoad() {
    super .viewDidLoad()

    imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.allowsEditing = true
}
override func viewDidAppear(_ animated: Bool) {

    if let _ = KeychainWrapper.standard.string(forKey: "uid") {
        performSegue(withIdentifier: "ToMessges", sender: nil)
        }
    }
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
        userImagePicker.image = image
        imageselected = true
    }else{
        print("Image wasent selected!")
        imagePicker.dismiss(animated: true, completion: nil)
    }
}
func setuser(img: String){
    let userData = [
        "username": userName!,"userimage": img]
    KeychainWrapper.standard.set(userId, forKey: "uid")
    let location = Database.database().reference().child("users").child(userId)
    location.setValue(userData)
    dismiss(animated: true, completion: nil)
}

func uploadImg (){
        if userNameField.text == nil {
            SignUpBtn.isEnabled = false

        }else {
            userName = userNameField.text
            SignUpBtn.isEnabled = true
        }

        guard let img = userImagePicker.image, imageselected == true else {
            print("Image needs to be selected")

            return
        }

        if let imgData = img.jpegData(compressionQuality: 0.2) {

            let imgUid = NSUUID().uuidString
            let metadata = StorageMetadata()
            metadata.contentType = "image/Jpeg"
            Storage.storage().reference().child(imgUid).putData(imgData, metadata:metadata){(metaData,error
                ) in

                if error != nil {
                print("Did not upload Image!")

            }else {
                   print("Uploaded")

                    let downloadURL = metadata.downloadURL()?.absoulteString      //error here 

                    if let url = downloadURL {
                        self.setUser(img:url)
                    }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

根据Firebase docdownloadURL类中可能没有直接属性StorageMetadata。因此,您可能必须使用dictionaryRepresentation并使用mediaLink键访问下载链接。

示例:

if let downloadURL = metadata.dictionaryRepresentation()["mediaLink"] as? String{

     self.setUser(img:downloadURL)
}