我正在使用 Kingfisher 框架来预取图像。 Kingfisher框架的链接是:https://github.com/onevcat/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!)
}
现在我想改变这一切,我想在预取后直接从缓存中选择,这样我就不必每次都下载图像,这会花费很多时间。有人可以帮我解决这个问题吗?多谢!
我也是 iOS 开发新手,我正在使用 kingfisher 来存储图像以进行缓存。
为了在预取图像时进入重点,有一个optionInfo
你留下它nil
。所以如果我猜对了你会从 URL 中预取图像,所以在这个optionInfo
您可以为每个图像设置一个缓存标识符,最好将图像的 URL 放在那里。
Kingfisher 文档非常清楚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()
}
}
因此,通过这种做法,您可以将它们存储到缓存中。要继续,您必须向我们展示您在预取后尝试过的内容。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)