Swift Firebase存储如何使用未知名称(NSUUID)检索图像 [英] Swift Firebase Storage How to retrieve image with unknow name(NSUUID)
问题描述
我正在制作一个函数来检索用户Image的url。但是,我的上传图像名称函数由NSUUID创建。因此,我不知道每个用户个人资料图片的名称是什么。我怎么能改善我的代码,以获得用户imgae为每个用户,而不是硬编码img名称?
pre $ 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) - >无效
if(error!= nil){
print(got no pic)
} else {
let profilePic:UIImage! = UIImage(data:data!)
self.imageV.image = profilePic
print(got pic)
}
}
}
路径是uid / profilePic / - < -file name - > -
上传函数
$ pre $ 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)!
让metaData = FIRStorageMetadata()
metaData.contentType =image / jpg
$ b $ let uploadTask = filePath.putData(imageData,metadata:metaData){如果让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(更改新的配置文件图片并上传成功)
}
}
有两种方法可以解决这个问题: b)在Firebase数据库中存储用户 profile_picture
的Firebase存储路径,并在您开始下载个人资料图片之前每次检索。 )
)2.每次将文件路径保存在 CoreData
中我们的用户上传一张个人资料图片,并每次点击 path
,以获得file_Path到您存储该用户的 profile_pic
的位置。 / b>
保存路径:
$ b $ pre $ func uploadSuccess(元数据:FIRStorageMetadata,存储路径:字符串)
{
print(upload succeded!)
print(storagePath)
NSUserDefaults.standardUserDefaults()。setObject(storagePath,forKey: storagePath.\((FIRAuth.auth()?。currentUser?.uid)!))
//将storagePath:(在firebase中的个人资料图片的文件路径)设置为每个用户的唯一密钥storagePath 。((FIRAuth.auth()?。currentUser?.uid)!)
NSUserDefaults.standardUserDefaults()。synchronize()
$ c
检索您的路径,每次开始下载图片时: - $ / $>
<$ p $让我们来看看如何使用存储路径来实现这个功能。 currentUser?.uid)!))as?字符串
注意: - 我正在使用 currentUser
ID,你可以使用USER SPECIFIC ID,如果你想下载多个用户的个人资料照片,你只需要把他们的 uid
放置就可以了。 $ b
I am making a function to retrieve the url as the user Image. However, my upload image name function created by NSUUID. Therefore, I would not know what is the name of each user profile picture. How could I improve my code to get the user imgae for every user instead of hard coding the img name?
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")
}
}
}
The path is uid/profilePic/--<-file name->--
upload function
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")
}
}
There can be two ways to go about this :-
1.) Store the Firebase Storage path of users profile_picture
in your Firebase database and retrieve every time before you start downloading your profile picture.
2.) Store the file path in CoreData
every time your user uploads a profile picture and hit that path
every time to get file_Path to where you stored that user's profile_pic
.
Storing the path :-
func uploadSuccess(metadata : FIRStorageMetadata , storagePath : String)
{
print("upload succeded!")
print(storagePath)
NSUserDefaults.standardUserDefaults().setObject(storagePath, forKey: "storagePath.\((FIRAuth.auth()?.currentUser?.uid)!)")
//Setting storagePath : (file path of your profile pic in firebase) to a unique key for every user "storagePath.\((FIRAuth.auth()?.currentUser?.uid)!)"
NSUserDefaults.standardUserDefaults().synchronize()
}
Retrieving your path, Every time you start downloading your image :-
let storagePathForProfilePic = NSUserDefaults.standardUserDefaults().objectForKey("storagePath.\((FIRAuth.auth()?.currentUser?.uid)!)") as? String
Note :- i am using currentUser
ID, you can use USER SPECIFIC id ,if you want to download multiple users profile pics, all you need to do is put their uid
in place.
这篇关于Swift Firebase存储如何使用未知名称(NSUUID)检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!