renodoo
renodoo

Reputation: 9

load image from firebase and caching swift3

I have a little problem. I get images from Firebase and I'm trying to add cache because I tried with this way but like this they are not cached:

I declared this variable

var uid:String = ""
var partitionUrl:String = ""
var titre:String = ""

And this to link to firebase.

let storageRef = FIRStorage.storage().reference()
let databaseRef = FIRDatabase.database().reference().child("canticles")

The app use the uid to get the title and the image and show it in UIlabel and UIImageView

@IBOutlet weak var titleCantique: UILabel!
@IBOutlet weak var partitionImageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    getPartitionImage()
}

override func viewWillAppear(_ animated: Bool) {
    print("partition uid : " + uid)
    print("partition URL : " + partitionUrl)
    print("partition TITLE : " + titre)
    self.titleCantique.text = titre
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func getPartitionImage(){

        DispatchQueue.main.async(execute: {
            self.storageRef.child(self.partitionUrl).data(withMaxSize: 10*1024*1024, completion: { (data, error) in

            if error != nil {
            print(error!)
            return

            }
            let image = UIImage(data: data!)
            self.partitionImageView.image = image
        })
    })
  }
}

The problem is when the phone is not connected, the images are not appearing. What could I do to solve this by adding a cache for the images then it will appears in offline?

Upvotes: 0

Views: 315

Answers (1)

renodoo
renodoo

Reputation: 9

I just proceeded like this

At first time, i added FirebaseStorageCache with cocoapod

In the swift file:

import UIKit
import Firebase
import FirebaseStorageCache

And implemented my getPartitionImage function like this

func getPartitionImage() {

    let ref: FIRStorageReference = self.storageRef.child(self.partitionUrl)

    FirebaseStorageCache.main.get(storageReference: ref) { data in

        self.partitionImageView.setImage(storageReference: ref)


    }
  }
}

Upvotes: 1

Related Questions