Picking images from cache after prefetching

不羁岁月 提交于 2019-12-24 00:14:59

问题


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:


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.



来源:https://stackoverflow.com/questions/37950034/picking-images-from-cache-after-prefetching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!