2016-06-21 16 views
2

我使用Kingfisher框架预取图像。到翠鸟框架的联系是: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() 
    } 
} 

我不知道如何为我相当新的应用程序开发中使用这个框架。一旦我预取了图像,我如何从缓存中获取这些预取图像。我想从缓存中选择这些图像,并将它们放入UIImage数组中。以前我没有从缓存中选择,但我每次都加载URL并将它们放入一个名为albumImagePathArray的字符串数组,并通过contentsOfURL将其转换为UIImage,以便将其放入albumImages中,如下面的代码所示:

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

现在我想改变这一切,我想预取,所以我不必每次下载这需要时间很多时间图像后,直接从缓存中挑选。任何人都可以帮助我吗?非常感谢!

+0

任何人都可以帮助我这个请! – laser2302

回答

2

我也是iOS Dev的新手,我使用翠鸟来存储图像缓存。

为了在预取图像的过程中考虑点,有一个optionInfo,您将其保留nil。因此,如果我猜对了,您可以从URL中预取图片,因此在此optionInfo中,您可以为每张图片设置一个缓存标识符,如果您在那里放置图片的网址,最好是。

翠鸟文档是相当明确的optionInfo所以你可以你的函数改变这种

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

所以这种做法,你可以将它们存储到缓存。要继续,您必须向我们展示您在预取后尝试过的内容。