Swift 写成的 UIImageView 的扩展:DPImageCache
jopen
9年前
DPImageCache 是 Swift 写成的 UIImageView 的扩展,能够异步加载图片然后缓存至本地文件。
Features
- Load image asynchronously.
- Cache local file-based.
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)