Ich entwickle eine App, wenn Benutzer anmelden können, auf der Benutzerprofilseite kann der Benutzer ein Bildprofil für die Show in meiner App auswählen. Dieses Bild wird in die Firebase-Datenbank hochgeladen und in Firebase Storage gespeichert. Ich möchte den Benutzer entscheiden, sein aktuelles Image-Profil zu löschen, wenn er möchte. Ich kann keinen Weg finden, es zu tun, wie kann ich sein Bild aus Firebase Storage löschen, verwende ich Metadaten, um es in Storage zu setzen.So löschen Sie ein Foto aus Firebase Storage
Hier ist mein Code für das Hochladen Bild in Firebase Lagerung und Firebase Datenbank:
// Create a path in order to save the photo in Firebase Database
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 profile in the Firebase Storage
func uploadImg() {
name = Auth.auth().currentUser?.displayName
userUid = Auth.auth().currentUser?.uid
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("Didn't upload image in Firebase Storage")
self.isUploaded = false
} else {
print("Uploaded in Firebase Storage")
self.isUploaded = true
let downloadURL = metadata?.downloadURL()?.absoluteString
if let url = downloadURL {
self.setUser(img: url)
self.downloadPhoto(user: self.name)
}
}
}
}
}
// The alert Controller (user can choose to take a photo with Camera or choose an image in his library then I use UIImagePickerController in order to display the image on a UIImageView)
@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)
}
Wie kann ich dieses Bild löschen, wenn ich einen Button „Foto löschen“ hinzufügen? Wie kann ich dieses Image in Firebase Storage abrufen? Ich kann das richtige Bild nicht finden und lösche es, und ich weiß wirklich nicht, wie ich einen Ordner in Firebase Storage erstellen kann, also befinden sich alle Benutzerbilder im selben Abschnitt des Speichers und ich kann den rechtes Bild (imageUid).
Ich denke, Sie können einen Ordner für Bilder wie folgt machen: 'Storage.storage(). Reference(). Kind (" YourImagesFolder "). Kind (imgUid) .putData (...' – 3stud1ant3
Wissen Sie, ob mit ein kostenloses Konto, ich kann einen Ordner erstellen, da kein Ordner in Firebase Storage erstellt wurde –