Picking images from cache after prefetching

2019-07-15 01:07发布

I'm using Kingfisher framework to prefetch images. The link to the Kingfisher framework is: https://github.com/onevcat/Kingfisher

Here's my code that I've written:

func downloadImages() {
    if(self.albumImagePathArray.isEmpty == false) {
        //dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
            let urls = self.albumImagePathArray.map { NSURL(string: $0)! }
            let prefetcher = ImagePrefetcher(urls: urls, optionsInfo: nil, progressBlock: nil, completionHandler: {
                (skippedResources, failedResources, completedResources) -> () in
                print("These resources are prefetched: \(completedResources)")
            })
        prefetcher.start()
    }
}

I'm not sure how to use this framework as I'm quite new to App development. Once I have prefetched the images, how do I get these prefetched images from cache. I want to pick these images from cache and put them into an array of UIImage. Previously I was not picking from cache, but I was loading the URLs every time and putting them an array of strings called albumImagePathArray and through contentsOfURL I was converting it into UIImage so it can be put into albumImages as shown in the code below:

var albumImages = [UIImage]()
for i in 0 ..< self.albumImagePathArray.count 
{
   let image = UIImage(data: NSData(contentsOfURL: NSURL(string: self.albumImagePathArray[i])!)!)
   self.albumImages.append(image!)
}

Now I want to change all that, I want to pick directly from cache after prefetching so I don't have to download the images every time which takes lot of time. Can anyone please help me on this? Thanks a lot!

1条回答
劫难
2楼-- · 2019-07-15 01:13

I'm also new at iOS Dev and I'm using kingfisher for storring images to cache.

To get into the point while you prefetch the images there is an optionInfo which you leave it nil. So if i guess right you prefetch images from URLs, so in this optionInfo you can set a Cache Identifier for each image, and would be best if you put there the URL of the image.

Kingfisher documentation is pretty clear about the optionInfo so you could change your function to this

func downloadImages() {

    if(self.albumImagePathArray.isEmpty == false) {
        let myCache = ImageCache(name: "the url of the images")
//dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
            let urls = self.albumImagePathArray.map { NSURL(string: $0)! }
            let prefetcher = ImagePrefetcher(urls: urls, optionsInfo: [.TargetCache(myCache)], progressBlock: nil, completionHandler: {
                (skippedResources, failedResources, completedResources) -> () in
                print("These resources are prefetched: \(completedResources)")
            })
        prefetcher.start()
    }
}

So with this practise you can store them to Cache. To continue you have to show us what you have tried after the prefetch.

查看更多
登录 后发表回答