为什么我在快速上传图片时遇到问题。零包装问题。网址

时间:2020-04-14 08:06:11

标签: swift firebase unwrap

**更新** 我的意思是我试图将我的profilePicLink与我从库上传的图像进行匹配。 也是selectedUsers是“用户类型”,如下所示:

var username : String = ""
var email : String = ""
var uid : String = ""
var profilePicLink : String  = ""

init(username : String, email: String, uid : String, profilePicLink: String ) {
    self.username = username
    self.email = email
    self.uid = uid
    self.profilePicLink = profilePicLink
}

尝试上传照片时遇到问题。动作是 我从图书馆拿照片

 @IBAction func getPhotoButton(_ sender: Any) {
        let image = UIImagePickerController()
        image.delegate = self
        image.sourceType = UIImagePickerController.SourceType.photoLibrary
        self.present(image, animated: true, completion: nil)
    }

它将我带到我的照片库。我选好照片后。我按以下代码单击“更新”按钮

 @IBAction func updatePhoto(_ sender: Any) {
        uploadPhoto()
    }
    func uploadPhoto(){
        selectedUser?.uploadProfileImage(imageView.image!){
            url in print (URL.self)
        }

    }

我得到的错误为**致命错误:在展开func uploadPhoto的图片时,意外地发现了nil,同时展开了一个可选值:** Fatal Error

这是我其他课程(用户)中的func代码,用于上传和获取个人资料图片

func getProfileImage() -> UIImage {
    if let url = NSURL(string: profilePicLink){
        if let data = NSData(contentsOf: url as URL) {
            return UIImage(data: data as Data)!
        }
    }
    return UIImage()
}

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 = image.jpegData(compressionQuality: 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)

        }
    }
}

已更新:我如下修改了函数uploadProfileImage。我的意思是我想将profilePicLink变量分配给downloadurl。然后更新profilePicLink的值

 func uploadProfileImage(_ image:UIImage, completion: @escaping ((_ url:URL?)->())) {

        let storageRef = Storage.storage().reference().child("profileImages").child("\(NSUUID().uuidString).jpg")


       guard let imageData = image.jpegData(compressionQuality: 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 (completion: {(url, error) in
                if error != nil {
                if let downloadurl = url?.absoluteString {
                if (self.profilePicLink == "") {
                self.profilePicLink = downloadurl
                   Database.database().reference().child("users").child(self.uid).updateChildValues(["profilePicLink":downloadurl])
                }
            }

        }   else {
            completion(nil)
            }
        }
            )
    }

        }

  }

请告知这一点。

0 个答案:

没有答案