Skip to content

pixel-ink/PIImageCache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

  • PIImageCache is an asynchronous image downloader with memory + disk caching
  • This memory cache is LRU cache, configurable size.
  • This disk cache can delete old files, on your controlled timing.
  • Useful extensions. (UIImageView and NSURL)
  • Written by Swift.

  • PIImageCacheは、非同期な画像のダウンロード、インメモリキャッシュ、ディスクキャッシュを提供します。
  • インメモリキャッシュはLRUCacheで、上限サイズは変更できます。
  • ディスクキャッシュは、一定期間経過した古いファイルを削除する方式です。
  • UIImageViewとNSURLにextensionを提供します。
  • Swift製です。

install

step1

  • manually
    • add PIImageCache.swift into your project
    • add PIImageCacheExtensions.swift into your project
  • cocoapods
    • add " pod 'PIImageCache', '1.0.0' " into your Podfile
    • add " import PIImageCache " into your code

step2

  • add this into your ViewController
override func didReceiveMemoryWarning() {
  PIImageCache.shared.allMemoryCacheDelete()
}

step3

  • add this into your AppDelegate
func applicationDidEnterBackground(application: UIApplication) {
  PIImageCache.shared.oldDiskCacheDelete()
}

basic useage

NSURL extension

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

UIImageView extension

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

download

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

advanced usage

prefetch (download to disk cache)

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

downloadWithId

set id, callback image with the id.

useful for 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
  }
}

configurable

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)!
  • default values
    • maxMemorySum = 10 // 10 images
    • limitByteSize = 3 * 1024 * 1024 //3MB
    • usingDiskCache = true
    • diskCacheExpireMinutes = 24 * 60 // 1 day
    • prefetchOprationCount = 5
    • cacheRootDirectory = NSTemporaryDirectory()
    • cacheFolderName = "PIImageCache"