gpt4 book ai didi

Swift Firebase Storage 如何检索名称未知的图像(NSUUID)

转载 作者:搜寻专家 更新时间:2023-11-01 06:18:33 26 4
gpt4 key购买 nike

我正在制作一个函数来检索 url 作为用户图像。但是,我的上传图片名称功能是由NSUUID 创建的。因此,我不知道每个用户个人资料图片的名称是什么。我如何改进我的代码以获得每个用户的用户 imgae 而不是硬编码 img 名称?

func getUserProfilePic(){
let uid = FIRAuth.auth()?.currentUser?.uid
let profilePath = refStorage.child("\(uid)").child("profilePic/xxxxx.jpg") // xxxxx = NSUUID

profilePath.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
if (error != nil) {
print("got no pic")
} else {

let profilePic: UIImage! = UIImage(data: data!)
self.imageV.image = profilePic
print("got pic")
}
}
}

路径为uid/profilePic/--<-文件名->--

上传功能

  func uploadingPhoto(){
let uid = FIRAuth.auth()?.currentUser?.uid
let imgName = NSUUID().UUIDString + ".jpg"
let filePath = refStorage.child("\(uid!)/profilePic/\(imgName)")
var imageData = NSData()
imageData = UIImageJPEGRepresentation(profilePic.image!, 0.8)!

let metaData = FIRStorageMetadata()
metaData.contentType = "image/jpg"

let uploadTask = filePath.putData(imageData, metadata: metaData){(metaData,error) in
if let error = error {
print(error.localizedDescription)
return
}else{
let downloadURL = metaData!.downloadURL()!.absoluteString
let uid = FIRAuth.auth()?.currentUser?.uid
let userRef = self.dataRef.child("user").child(uid!)
userRef.updateChildValues(["photoUrl": downloadURL])
print("alter the new profile pic and upload success")

}

}

最佳答案

我强烈建议同时使用 Firebase 存储和 Firebase 实时数据库来存储 UUID -> URL 映射,并“列出”文件。 Realtime Database will handle offline use cases也像 Core Data,所以真的没有理由使用 Core Data 或 NSUserDefaults。下面是显示这些部分如何交互的一些代码:

共享:

// Firebase services
var database: FIRDatabase!
var storage: FIRStorage!
...
// Initialize Database, Auth, Storage
database = FIRDatabase.database()
storage = FIRStorage.storage()

上传:

let fileData = NSData() // get data...
let storageRef = storage.reference().child("myFiles/myFile")
storageRef.putData(fileData).observeStatus(.Success) { (snapshot) in
// When the image has successfully uploaded, we get it's download URL
let downloadURL = snapshot.metadata?.downloadURL()?.absoluteString
// Write the download URL to the Realtime Database
let dbRef = database.reference().child("myFiles/myFile")
dbRef.setValue(downloadURL)
}

下载:

let dbRef = database.reference().child("myFiles")
dbRef.observeEventType(.ChildAdded, withBlock: { (snapshot) in
// Get download URL from snapshot
let downloadURL = snapshot.value() as! String
// Create a storage reference from the URL
let storageRef = storage.referenceFromURL(downloadURL)
// Download the data, assuming a max size of 1MB (you can change this as necessary)
storageRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
// Do something with downloaded data...
})
})

有关详细信息,请参阅 Zero to App: Develop with Firebase , 它是 associated source code ,有关如何执行此操作的实际示例。

关于Swift Firebase Storage 如何检索名称未知的图像(NSUUID),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38937740/

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