Are my images auto cached using Alamofire and AlamofireImage

6.4k views Asked by At

New to AFImage. I'm not sure if this is the correct way to grab an image and have it cached. Seems like it is not hitting the server everytime this is running but I'm not sure if it is being cached? I'm getting lucky? It also appears the the syntax I'm using below is dated...

Any comment appreciated.

  Alamofire.request("https://www.website.com/advertising/images/"+whichad!)
    .responseImage { response in                                 

                    if let image = response.result.value {
                        //print("image downloaded: \(image)")
                        self.ad1image.image = image
                    }
                    else{
                        self.ad1image.image = UIImage(named: "TWITlogoSQ")
                    }
                }`
1

There are 1 answers

0
0x384c0 On

When you load images with Alamofire.request("url").responseImage, downloaded images will be cached only with URLCache, which behaviour depends of Cache-Control headers and URLCache has some limitations

Its better to use af_setImage(). This method uses a combination of an URLCache and AutoPurgingImageCacheto create a very robust, high performance image caching system.

for example

import AlamofireImage

...

ad1image.af_setImage(
    withURL: URL(string:"https://www.website.com/advertising/images/"+whichad!)!,
    placeholderImage: UIImage(named: "TWITlogoSQ")
)