Uiimageview获取图像后什么也没显示

时间:2018-12-24 08:52:51

标签: swift firebase firebase-storage

您好,我是Xcode的新手。我正在尝试获取存储在Firebase中的图像。但是,当我尝试放置代码时,它什么也没显示。我正在尝试打印图像,没有显示。有很多折磨,但我不了解其中的每一个。

func loadUserDetail(){

    ref = Database.database().reference()
    imgStorage = Storage.storage().reference()

    let userID = Auth.auth().currentUser?.uid
    ref.child("User").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
        // Get user value
        let value = snapshot.value as? NSDictionary
        let username = value?["name"] as? String ?? ""
        let email = value?["email"] as? String ?? ""
        let address = value?["address"] as? String ?? ""
        let image = value?["image"] as? String ?? ""
        let phone = value?["phone"] as? String ?? ""
        let user = User(name: username, email: email, address: address, image : image, phone : phone)

        self.fullnameTF.text = user.name
        self.emailTF.text = user.email
        self.PhoneTF.text = user.phone

        let storageRef = self.imgStorage.child("User/\(userID!)/UserPic")
        storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
            if let error = error {
                print("error: \(error.localizedDescription)")
            } else {
                let image = UIImage(data: data!)
                self.imgProfilePicture.image = image
            }
        }




    }) { (error) in
        print(error.localizedDescription)
    }

}

我认为问题是当我上传从选择器获得的图像时,显示图像具有0kb。

 let fileData = NSData() // get data...
        let storageRef = self.imgStorage.child("User/\(userUiD!)/UserPic")
        storageRef.putData(fileData as Data).observe(.success) { (snapshot) in
            // When the image has successfully uploaded, we get it's download URL
            storageRef.downloadURL(completion: { (url, error) in
                if (error == nil) {
                    if let downloadUrl = url {
                        // Make you download string
                        let downloadString = downloadUrl.absoluteString

                         self.ref.child("User").child(userUiD!).child("image").setValue(downloadString)

                    }
                } else {
                   print("Error To Saved")
                }
            })



        }

    }

1 个答案:

答案 0 :(得分:1)

您应将let fileData = NSData()替换为图像数据。 NSData()仅创建具有0kb的空数据对象,因此您应该将图像转换为数据:

 var fileData = UIImagePNGRepresentation(image)  // image is UIImage which you get from Picker

or 

 var fileData = UIImageJPEGRepresentation(image, 0.7)