Swift 写成的 UIImageView 的扩展:DPImageCache

jopen 9年前

DPImageCache 是 Swift 写成的 UIImageView 的扩展,能够异步加载图片然后缓存至本地文件。

Features

  • Load image asynchronously.
  • Cache local file-based.

alt tag

How to use

  • Import DPImageCache.swift

  • Set default cache directory like this:

let dirName = "cache"  func createCacheDirectory() {          var fileMan = NSFileManager.defaultManager()          var cacheDir = (NSSearchPathForDirectoriesInDomains(              .DocumentDirectory,              .UserDomainMask, true)[0] as! NSString)              .stringByAppendingPathComponent(dirName)          if !fileMan.fileExistsAtPath(cacheDir) {              fileMan.createDirectoryAtPath(                  cacheDir,                  withIntermediateDirectories: false,                  attributes: nil,                  error: nil)          }      }

  • Simple code like this:
    var imgAddress = "http://blabla.com/blabla.png"      imgView.setImageCacheWithAddress(              imgAddress,              placeHolderImage: UIImage(named: "placeHolderImage")!,              cacheDirName: dirName)

项目主页:http://www.open-open.com/lib/view/home/1435374152138