PIImageCache 1.0.0

PIImageCache 1.0.0

测试已测试
语言语言 SwiftSwift
许可 MIT
发布最后发布2015年6月
SPM支持 SPM

pixelink 维护。



  • PIImageCache 是一个具有内存和磁盘缓存的异步图像下载器
  • 此内存缓存是 LRU 缓存,可配置大小。
  • 此磁盘缓存可以按照您的控制时间删除旧文件。
  • 有用的扩展。(UIImageView 和 NSURL)
  • 使用 Swift 编写。


  • PIImageCache提供非同步的图像下载、内存和磁盘缓存。
  • 内存缓存是LRUCache,最大大小可配置。
  • 磁盘缓存采用一定时间后过期的旧文件的删除方式。
  • 为 UIImageView 和 NSURL 提供扩展。
  • Swift语言编写。

安装

步骤1

  • 手动
    • 将 PIImageCache.swift 添加到您的项目中
    • 将 PIImageCacheExtensions.swift 添加到您的项目中

  • cocoapods
    • 将“ pod ‘PIImageCache’, '1.0.0’ ”添加到 Podfile 中
    • 在代码中添加“ import PIImageCache ”

步骤2

  • 将以下代码添加到 ViewController 中
override func didReceiveMemoryWarning() {
  PIImageCache.shared.allMemoryCacheDelete()
}

步骤3

  • 将以下代码添加到 AppDelegate 中
func applicationDidEnterBackground(application: UIApplication) {
  PIImageCache.shared.oldDiskCacheDelete()
}

基本用法

NSURL 扩展

let url = NSURL(string: "http://place-hold.it/200x200")!
let image = url.getImageWithCache()

UIImageView 扩展

let url = NSURL(string: "http://place-hold.it/200x200")!
let imgView = UIImageView()
imgView.imageOfURL(url)

下载

let url = NSURL(string: "http://place-hold.it/200x200")!
let cache = PIImageCache.shared
image = cache.get(url)!

高级用法

预取(下载到磁盘缓存)

let url = NSURL(string: "http://place-hold.it/200x200")!
let cache = PIImageCache.shared
cache.prefetch(url)

downloadWithId

设置 id,使用 id 获取回调图像。

这对 UITableViewCell 很有用。

// example: code in cellForRowAtIndexPath
let url = NSURL(string: "http://lorempixel.com/200/200/" )!
let id = indexPath.row
cell.id = indexPath.row
PIImageCache.shared.getWithId(url, id: i) {
  [weak self] id, image in
  if id == cell.id {
    cell.icon.image = image
  }
}

可配置的

let cache = PIImageCache.shared
var config = PIImageCache.Config()
config.maxMemorySum = 5
config.limitByteSize = 100 * 1024 // 100kB
cache.setConfig(config)

let url = NSURL(string: "http://place-hold.it/200x200")!
let image = cache.get(url)!
  • 默认值
    • maxMemorySum = 10 // 10 个图像
    • limitByteSize = 3 * 1024 * 1024 //3MB
    • usingDiskCache = true
    • diskCacheExpireMinutes = 24 * 60 // 1 天
    • prefetchOprationCount = 5
    • cacheRootDirectory = NSTemporaryDirectory()
    • cacheFolderName = “PIImageCache”