[英]Swift delete image from firebase storage
I have a function that deletes an object from my firebase database by swiping the tableView cell, however, my tableView cells also contain images that are saved in firebase storage, I would like for the image to be deleted from the storage too when the data is deleted from the database, how can I accomplish this? 我有一个功能,可以通过滑动tableView单元格从我的Firebase数据库中删除一个对象,但是,我的tableView单元格还包含保存在Firebase存储中的图像,我也希望在数据为从数据库中删除后,我该怎么做?
Code for deleting: 删除代码:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let name = food[indexPath.row].name
let ref = Database.database().reference().child("Recipes")
ref.queryOrdered(byChild: "Name").queryEqual(toValue: name).observe(.childAdded, with: { (snapshot) in
//Removes deleted cell from firebase
snapshot.ref.removeValue(completionBlock: { (error, reference) in
if error != nil {
print("There has been an error: \(error)")
}
//Removes deleted cell from array
food.remove(at: indexPath.row)
//Removes deleted cell from tableView
tableView.deleteRows(at: [indexPath], with: .left)
})
})
}
}
Code for loading: 加载代码:
let parentRef = Database.database().reference().child("Recipes")
let storage = Storage.storage()
parentRef.observe(.value, with: { snapshot in
if ( snapshot.value is NSNull ) {
// DATA WAS NOT FOUND
print("– – – Data was not found – – –")
} else {
//Clears array so that it does not load duplicates
food = []
// DATA WAS FOUND
for user_child in (snapshot.children) {
let user_snap = user_child as! DataSnapshot
let dict = user_snap.value as! [String: String?]
//Defines variables for labels
let recipeName = dict["Name"] as? String
let recipeDescription = dict["Description"] as? String
let downloadURL = dict["Image"] as? String
let storageRef = storage.reference(forURL: downloadURL!)
storageRef.getData(maxSize: 1 * 1024 * 1024) { (data, error) -> Void in
let recipeImage = UIImage(data: data!)
food.append(Element(name: recipeName!, description: recipeDescription!, image: recipeImage!))
self.tableView.reloadData()
}
}
}
})
Would also really appreciate if someone could also help me with this other question I asked about this same app: Convert observe .value to .childAdded in swift 如果有人也可以帮助我解决有关该应用程序的其他问题,我也将非常感谢: 将observe .value转换为.child
Edit: 编辑:
I have added the URL to the array when loading the objects from firebase: 从firebase加载对象时,我已将URL添加到数组:
food.append(Element(name: recipeName!, description: recipeDescription!, image: recipeImage!, downloadURL: downloadURL!))
And this is what I'm trying to use to delete: 这就是我要用来删除的内容:
let storage = Storage.storage()
let storageRef = storage.reference()
let desertRef = storageRef.child(food[indexPath.row].downloadURL)
//Removes image from storage
desertRef.delete { error in
if let error = error {
print(error)
} else {
// File deleted successfully
}
}
I don't think it's finding the image, however... I get this error: 我认为找不到图像,但是...我收到此错误:
Error Domain=FIRStorageErrorDomain Code=-13010 "Object https:/firebasestorage.googleapis.com/v0/b/recipe-app-1b76e.appspot.com/o/B74F604B-68FD-45BB-ABDB-150B03E83A2A.png?alt=media&token=ae2643c4-6479-4dc8-b389-d04caac98392 does not exist."
错误域= FIRStorageErrorDomain代码= -13010“对象https:/firebasestorage.googleapis.com/v0/b/recipe-app-1b76e.appspot.com/o/B74F604B-68FD-45BB-ABDB-150B03E83A2A.png?alt = media&token = ae2643c4-6479-4dc8-b389-d04caac98392不存在。” UserInfo={object=https:/firebasestorage.googleapis.com/v0/b/recipe-app-1b76e.appspot.com/o/B74F604B-68FD-45BB-ABDB-150B03E83A2A.png?alt=media&token=ae2643c4-6479-4dc8-b389-d04caac98392, bucket=recipe-app-1b76e.appspot.com, ResponseBody={ "error": { "code": 404, "message": "Not Found. Could not delete object" } },
UserInfo = {object = https:/firebasestorage.googleapis.com/v0/b/recipe-app-1b76e.appspot.com/o/B74F604B-68FD-45BB-ABDB-150B03E83A2A.png?alt = media&token = ae2643c4-6479- 4dc8-b389-d04caac98392,bucket = recipe-app-1b76e.appspot.com,ResponseBody = {“ error”:{“ code”:404,“ message”:“未找到。无法删除对象”}},
Solved it! 解决了! Heres what worked for me:
这对我有用:
let storage = Storage.storage()
let url = food[indexPath.row].downloadURL
let storageRef = storage.reference(forURL: url)
//Removes image from storage
storageRef.delete { error in
if let error = error {
print(error)
} else {
// File deleted successfully
}
}
create reference based on image location. 根据图片位置创建参考。
// Create a reference to the file to delete
let imageRef = storageRef.child("image.png")
// Delete the file
imageRef.delete { error in
if let error = error {
// Uh-oh, an error occurred!
} else {
// File deleted successfully
}
}
add the above code before following line 在下面的行之前添加上面的代码
food.remove(at: indexPath.row)
Storing/Downloading/Deleting images or videos Using Firebase Cloud Storage ! 使用Firebase Cloud Storage存储/下载/删除图像或视频 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.