gpt4 book ai didi

swift - Uiimageview 获取图像后什么都不显示

转载 作者:行者123 更新时间:2023-11-28 13:55:35 24 4
gpt4 key购买 nike

我是 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")
}
})
}}

最佳答案

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

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

or

var fileData = UIImageJPEGRepresentation(image, 0.7)

关于swift - Uiimageview 获取图像后什么都不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53911207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com