2016-08-13 39 views
1

我正在製作一個函數來檢索URL作爲用戶Image。但是,我的上傳圖像名稱函數由NSUUID創建。因此,我不知道每個用戶個人資料圖片的名稱是什麼。我怎樣才能改善我的代碼,讓每個用戶的用戶imgae而不是硬編碼img名稱?Swift Firebase存儲如何檢索具有未知名稱(NSUUID)的圖像

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/- < -file名稱 - > -

上傳功能

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") 

    } 

} 
+1

顯示上傳圖像功能代碼... –

+0

@AaronSaunders更新 –

回答

1

可以有兩種方式去了解這一點: -

1.)將用戶的Firebase存儲路徑profile_picture存儲在您的Firebase數據庫中,並在您開始下載個人資料圖片之前每次檢索。

2)每個用戶上傳資料圖片時間存儲文件路徑CoreData和打的path每次拿到FILE_PATH到您存儲該用戶的profile_pic

存儲路徑: -

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() 

} 

檢索您的路徑,每次啓動下載你的形象: -

let storagePathForProfilePic = NSUserDefaults.standardUserDefaults().objectForKey("storagePath.\((FIRAuth.auth()?.currentUser?.uid)!)") as? String 

注: - 我使用currentUser ID,您可以使用用戶特定ID,如果你想下載多個用戶的個人資料圖片,你需要做的就是把他們的uid到位。

+0

感謝您回答我的問題,您是否建議將每個用戶的詳細信息存儲在NSUserDefault中? –

+1

不只是這些文件路徑,你必須在你的後端搜索...休息取決於你... – Dravidian

3

我強烈建議您一起使用Firebase存儲和Firebase實時數據庫來存儲該UUID - > URL映射以及「列出」文件。像Core Data這樣的Realtime Database will handle offline use cases也是如此,所以真的沒有理由與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,爲一個如何做到這一點的實際例子。