0

To reduce the amount of calls to firebase, I want to save the image on the phone after the first time its retrieved from firebase. Currently I have it so that the image is in storage and it is getting called everytime the view is loaded.

    func setImageToProfilePicture(){
    downloadImages(success: { (image) in
        print(image)
    }) { (error) in
        print(error.localizedDescription)
    }
}

func downloadImages(success:@escaping (_ image:UIImage)->(),failure:@escaping (_ error:Error)->()){
    // Create a reference with an initial file path and name
    guard let uid = Auth.auth().currentUser?.uid else { return }
    let reference = Storage.storage().reference(withPath: "user/\(uid)")

    reference.getData(maxSize: (1 * 1024 * 1024)) { (data, error) in
            if let _error = error{
                print(_error)
                failure(_error)
            } else {
                if let _data  = data {
                    let myImage:UIImage! = UIImage(data: _data)
                    self.profileImage.image = myImage
                    success(myImage)
                }
            }
        }
} 

I am not sure how to save the data on the phone after this is done.

OmarSaid
  • 25
  • 7
  • You can save image in document directory with the help of this link : https://stackoverflow.com/questions/32836862/how-to-use-writetofile-to-save-image-in-document-directory – Khushbu Nov 28 '18 at 04:19
  • if i am getting the image from firebase storage, can I still use this to save it once I retrieved the image for the first time? – OmarSaid Nov 28 '18 at 12:53

1 Answers1

0

I suggest that you should cache images instead of save to Local disk and remember that to set memory/ disk cache limit age, cost for save device memory. See this library for more. https://github.com/pinterest/PINCache Note more: Choose carefully where you will cache(Ram or Disk) for each image.

Lê Minh
  • 56
  • 3