我正在尝试从Firebase下载照片用户帐户。 我解释了我的问题: 在用户的profil页面上,有一个按钮显示他的profil头像的当前图像,首先在按钮背景中没有图像,当他第一次点击时他可以在他的照片中选择图像库或通过设备摄像头拍摄照片(通过警报控制器),然后将此图像或照片上载到Firebase存储中,并且新照片显示在按钮背景上。如何从Firebase存储下载照片并替换当前用户的最后一张
但是,当用户想要改变他的当前照片时,他再次点击该按钮并选择或拍照,但是在他做了这些之后,按钮背景不会改变,这意味着用户改变他的照片,但是在当前页面照片不会更新更改,但如果我转到另一个视图并返回更改照片视图,则用户刚选择的正确照片位于按钮背景中。因此,如果用户没有转到另一个视图,下载可能会很好,但不会立即生效。这有点难以解释。
这是我为我的上传方式代码:
// Variables I need
@IBOutlet weak var photoProfil: UIButton!
var name: String!
var userUid: String!
@IBOutlet var userImagePicker: UIImageView!
var imagePicker: UIImagePickerController!
var imageSelected = false
var isUploaded: Bool = false
var refDatabase: DatabaseReference!
var photovide: UIImage = UIImage(named:"profilvide")!
// Take a photo with the camera (first choice)
func takePhoto(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePicker.delegate = self
imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.camera // Source Camera
self.present(imagePicker, animated: false, completion: nil)
} else { // If camera doesn't work
imagePicker.delegate = self
imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary // Source Library
self.present(imagePicker, animated: false, completion: nil)
}
}
// Choose an image from the library
func libraryPhoto(sender: AnyObject) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
image.allowsEditing = false
self.present(image, animated: true)
}
// Take this image and shows it on the View Controller
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
// I want to upload the photo to storage and save it in the database
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
photoProfil.setImage(image, for: .normal) // Button showing the user photo
userImagePicker.image = image
imageSelected = true
uploadImg()
} else {
print("Error...")
}
imagePicker.dismiss(animated: true, completion: nil)
self.dismiss(animated: true, completion: nil)
}
// The alert Controller
@IBAction func actionButton(_ sender: Any) {
let attributedString = NSAttributedString(string: "User photo", attributes: [
NSFontAttributeName : UIFont.boldSystemFont(ofSize: 15),
NSForegroundColorAttributeName : UIColor.black
])
let alertController = UIAlertController(title: "", message: "", preferredStyle: .actionSheet)
alertController.message = nil
alertController.setValue(attributedString, forKey: "attributedTitle")
alertController.addAction(UIAlertAction(title: "Take photo", style: .default, handler: self.takePhoto))
alertController.addAction(UIAlertAction(title: "Choose in Library", style: .default, handler: self.libraryPhoto))
alertController.addAction(UIAlertAction(title: "Show current photo", style: .default, handler: self.showPhoto))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
// Create a path in order to save the photo in Firebase
func setUser(img: String) {
var userUid = Auth.auth().currentUser?.uid
let userData = [
"nickname": Auth.auth().currentUser?.displayName,
"userImg": img
]
KeychainWrapper.standard.set(userUid!, forKey: "uid")
let location = Database.database().reference().child("users").child(userUid!).child("pseudo")
location.setValue(userData)
dismiss(animated: true, completion: nil)
}
// Upload and put image in the Firebase Storage
func uploadImg() {
guard let img = userImagePicker.image, imageSelected == true else {
print("Image needs to be selected")
return
}
if let imgData = UIImageJPEGRepresentation(img, 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 img")
self.isUploaded = false
} else {
print("uploaded")
self.isUploaded = true
let downloadURL = metadata?.downloadURL()?.absoluteString
if let url = downloadURL {
self.setUser(img: url)
}
}
}
}
}
下面是下载的方式,并显示照片下载后的代码:)在
override func viewDidLoad() {
super.viewDidLoad()
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.allowsEditing = true
// For download photo from Firebase
name = Auth.auth().currentUser?.displayName
userUid = Auth.auth().currentUser?.uid
refDatabase = Database.database().reference().child("users").child(userUid!)
refDatabase.child("pseudo").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild("userImg") {
self.isUploaded = true
print("True userImg exists")
self.downloadPhoto(user: self.name)
} else {
self.isUploaded = false
self.userImagePicker = UIImageView(image: self.photovide)
self.photoProfil.setImage(self.photovide, for: .normal)
print("isUploaded2 = false")
print("false userImg doesn't exist")
print("Aucune photo n'a été uploadée dans la base de données")
}
})
}
override func viewWillAppear(_ animated: Bool) {
self.userImagePicker.image = nil
guard let username = Auth.auth().currentUser?.displayName else { return }
self.navigationItem.title = username
refDatabase = Database.database().reference().child("users").child(userUid!)
refDatabase.child("pseudo").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild("userImg") {
self.isUploaded = true
print("isUploaded2 = true")
print("true userImg exists")
self.downloadPhoto(user: self.name)
} else {
self.isUploaded = false
self.userImagePicker = UIImageView(image: self.photovide)
self.photoProfil.setImage(self.photovide, for: .normal)
print("False userImg doesn't exist")
}
})
}
func downloadPhoto(user: String) {
self.name = user
let recipientData = Database.database().reference().child("users").child(userUid!).child("pseudo")
recipientData.observeSingleEvent(of: .value, with: { (snapshot) in
let data = snapshot.value as! Dictionary<String, AnyObject>
let nickname = data["nickname"]
let userImg = data["userImg"]
let ref = Storage.storage().reference(forURL: userImg! as! String)
ref.getData(maxSize: 1000000, completion: { (data, error) in
if error != nil {
print("Could not load image")
} else {
if let imgData = data {
if let img = UIImage(data: imgData) {
self.userImagePicker.image = img
self.photoProfil.setImage(img, for: .normal)
}
}
}
})
})
userImagePicker.reloadInputViews()
}
func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
self.navigationController?.isNavigationBarHidden = false
self.tabBarController?.tabBar.isHidden = false
sender.view?.removeFromSuperview()
}
// Try to do the same with the viewDidLoad() method for download but can't work
override func viewDidAppear(_ animated: Bool) {
refDatabase = Database.database().reference().child("users").child(userUid!)
refDatabase.child("pseudo").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild("userImg") {
self.isUploaded = true
print("True userImg exists")
self.downloadPhoto(user: self.name)
} else {
self.isUploaded = false
self.userImagePicker = UIImageView(image: self.photovide)
self.photoProfil.setImage(self.photovide, for: .normal)
print("False userImg doesn't exist")
}
})
}
好的,如果你在'photoProfil.setImage(image,for:.normal)''didFinishPickingMediaWithInfo'方法内部添加'print(image)',那它会打印什么?所以我们可以肯定,这种方法被称为 – 3stud1ant3
好吧,我做到了,我添加了打印(图像),但没有出现,所以我将其替换为print(“image”)并在控制台中打印 –
您在'如果让'陈述? – 3stud1ant3