Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any way to preload images into the cache? #104

Closed
tfe opened this issue Apr 28, 2016 · 6 comments
Closed

Any way to preload images into the cache? #104

tfe opened this issue Apr 28, 2016 · 6 comments

Comments

@tfe
Copy link

tfe commented Apr 28, 2016

Let's say I have an array of image URLs that I know I'm going to need soon... is there any way to preload these into the cache so that they can start downloading immediately?

@dingua
Copy link

dingua commented May 1, 2016

i am doing the same, for that i download the images and i add them to the cache AutoPurgingImageCache by the method addImage , next time when i need to access to the image i check if it is in the cache or not using the method imageWithIdentifier and i give as parameter the image's identifier that i used when adding it to the cache.

@kishorer747-zz
Copy link

kishorer747-zz commented May 27, 2016

Yes, you can download all the images and add them to cache individually. But it would have been great if there was a method to directly download and cache a list of image URL's
And you don't need to check if the image is in cache or not before using it.
From the docs: https://github.com/Alamofire/AlamofireImage#setting-image-with-url

let imageView = UIImageView(frame: frame)
let URL = NSURL(string: "https://httpbin.org/image/png")!

imageView.af_setImageWithURL(URL)

If the image is cached locally, the image is set immediately.

@kcharwood
Copy link
Contributor

Yes you can certainly do this.

On the Image Downloader, call downloadImages. You can fire and forget, and if they get back in time they will pull from cache.

@kishorer747-zz
Copy link

@kcharwood @kean
If i use downloadImages by sending a list of URL's, on completion of download of all images, will these images be added to cache? And if so, the forRequest parameter we used to send for .downloadImage is not there in downloadImages method

Basically i am asking if i fire and forget, will these images be added to cache and next time the same url is accessed, the image is loaded from cache?

@bendecoste
Copy link

Basically i am asking if i fire and forget, will these images be added to cache and next time the same url is accessed, the image is loaded from cache?

You can do this. As long as the image downloader instance you are using is the same as the one you use to download the images later. In my case an implementation looked like this

// precaching done here
UIImageView.af_sharedImageDownloader.downloadImages(URLRequests: requests)

// later, when setting content on some imageview
// as long as url was in the array of URLRequests above, the cached version will be used
imageView.af_setImageWithURL(url, placeholderImage: UIImage(named: "grey-image-placeholder"), filter: nil, imageTransition: .CrossDissolve(0.5))

@cnoon
Copy link
Member

cnoon commented Aug 22, 2016

Thanks @kcharwood and @bendecoste! I'm going to close this issue out since I believe all the open questions have been addressed.

Cheers. 🍻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants