gpt4 book ai didi

ios - 如何将图像存储在 firebase 存储中并将它们的链接保存在数据库中?

转载 作者:行者123 更新时间:2023-11-28 11:49:43 25 4
gpt4 key购买 nike

我四处寻找答案。我得到的最接近的是 here但是,它并没有完全回答我的问题。也就是说,如何在数据库中存储对保存在 firebase 存储中的图像的引用。

下面是我试过的代码。它能够在上传时存储一张图片,但我不确定这是否是存储引用的意思。

if let imageData = UIImageJPEGRepresentation(image, 0.8) {
let metadata = storageRef //.child("poop/")

let uploadTask = metadata.putData(imageData, metadata: nil) {
(metadata, error) in
guard let metadata = metadata else {
// Uh-oh, an error occurred!
return
}

// You can also access to download URL after upload.
storageRef.downloadURL {
(url, error) in
guard let downloadURL = url else {
// Uh-oh, an error occurred!
return
}
//let imgURL = url

//database integration
let ref = Database.database().reference()
let usersRef = ref.child("usersPosts")

let uid = Auth.auth().currentUser?.uid
let newUserRef = usersRef.child(uid!)
//creates a child for email and password (i think we shud store password so we can tell sumone what it is inmediatly, maybe)
newUserRef.setValue(["Image": "\(downloadURL)"])
}

}

// let imgURL = storageRef.downloadURL
//
// //database integration
// let ref = Database.database().reference()
// let usersRef = ref.child("usersPosts")
//
// let uid = Auth.auth().currentUser?.uid
// let newUserRef = usersRef.child(uid!)
// //creates a child for email and password (i think we shud store password so we can tell sumone what it is inmediatly, maybe)
//// newUserRef.setValue(["Image": "\(imgURL)"])





// For progress
uploadTask.observe(.progress, handler: { (snapshot) in
guard let progress = snapshot.progress else {
return
}

let percentage = (Float(progress.completedUnitCount) / Float(progress.totalUnitCount))
progressBlock(Double(percentage))
})

} else {
completionBlock(nil, "Image could not be converted to Data.")
}

感谢您的帮助!

最佳答案

请按要求修改你的代码

var imgData: NSData = NSData(data: UIImageJPEGRepresentation((self.img_Photo?.image)!, 0.8)!)
self.uploadProfileImageToFirebase(data: imgData)


func uploadProfileImageToFirebase(data:NSData){
let storageRef = Storage.storage().reference().child("usersPosts").child("\(uid).jpg")
if data != nil {
storageRef.putData(data as Data, metadata: nil, completion: { (metadata, error) in
if(error != nil){
print(error)
return
}
guard let userID = Auth.auth().currentUser?.uid else {
return
}
// Fetch the download URL
storageRef.downloadURL { url, error in
if let error = error {
// Handle any errors
if(error != nil){
print(error)
return
}
} else {
// Get the download URL for 'images/stars.jpg'

let urlStr:String = (url?.absoluteString) ?? ""
let values = ["downloadURL": urlStr]
self.addImageURLToDatabase(uid: userID, values: values as [String : AnyObject])
}
}
})
}

}


func addImageURLToDatabase(uid:String, values:[String:AnyObject]){
let ref = Database.database().reference(fromURL: "https://exampleapp.firebaseio.com/")
let usersReference = ref.child("usersPosts").child((Auth.auth().currentUser?.uid)!)

usersReference.updateChildValues(values) { (error, ref) in
if(error != nil){
print(error)
return
}
self.parentVC?.dismiss(animated: true, completion: nil)
}

}

关于ios - 如何将图像存储在 firebase 存储中并将它们的链接保存在数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51974047/

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