Reputation: 5
I'm new at Xcode. I'm trying to get an image that's stored in firebase. But. when I try to put the code, it's showing nothing. I'm trying to print the image, its nil. There are a lot of tutorials, but every one of them I don't understand.
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)
}
}
I think the problem is when I'm uploading the image that I get from picker: it's showing that the image size is 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")
}
})
}}
Upvotes: 0
Views: 58
Reputation: 539
you should replace let fileData = NSData()
with your image data . NSData() create only empty data object with 0kb so you should convert your image to data like :
var fileData = UIImagePNGRepresentation(image) // image is UIImage which you get from Picker
or
var fileData = UIImageJPEGRepresentation(image, 0.7)
Upvotes: 1