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.