Swift Firebase Storage How to retrieve image with unknow name(NSUUID)

后端 未结 2 1214
伪装坚强ぢ
伪装坚强ぢ 2021-01-16 07:03

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

相关标签:
2条回答
  • 2021-01-16 07:18

    I highly recommend using Firebase Storage and the Firebase Realtime Database together to store that UUID -> URL mapping, and to "list" files. The Realtime Database will handle offline use cases like Core Data as well, so there's really no reason to futz with Core Data or NSUserDefaults. Some code to show how these pieces interact is below:

    Shared:

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

    Upload:

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

    Download:

    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...
      })
    })
    

    For more information, see Zero to App: Develop with Firebase, and it's associated source code, for a practical example of how to do this.

    0 讨论(0)
  • 2021-01-16 07:34

    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.

    0 讨论(0)
提交回复
热议问题