预取后从缓存中选择图像 [英] Picking images from cache after prefetching

查看:137
本文介绍了预取后从缓存中选择图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Kingfisher框架预取图像.翠鸟框架的链接为: https://github.com/onevcat/Kingfisher

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

这是我编写的代码:

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()
    }
}

我不确定如何使用此框架,因为我对App开发还很陌生.预取图像后,如何从缓存中获取这些预取图像.我想从缓存中选择这些图像,并将它们放入UIImage数组中.以前我不是从缓存中挑选,而是每次都加载URL并将它们放入一个称为AlbumImagePathArray的字符串数组,并通过contentsOfURL将其转换为UIImage以便可以将其放入albumImages中,如下面的代码所示:

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!

推荐答案

我也是iOS Dev的新手,我正在使用Kingfisher存储图像以进行缓存.

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

要预读图像时要弄清楚点,有一个optionInfo,您将其保留为nil.因此,如果我猜对了,您可以从URL预取图像,所以在此optionInfo中,您可以为每个图像设置一个缓存标识符,最好在其中放置图像的URL.

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文档对optionInfo非常清楚,因此您可以将功能更改为此

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()
    }
}

因此,通过这种做法,您可以将它们存储到Cache中.要继续,您必须向我们展示您在预取之后尝试过的操作.

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

这篇关于预取后从缓存中选择图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆