1.获取缓存大小
//获取缓存大小
func getCacheSize() -> String {
//cache文件夹
let cachePath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first
//文件夹下所有文件
let files = FileManager.default.subpaths(atPath: cachePath!)!
//遍历计算大小
var size = 0
for file in files {
//文件名拼接到路径中
let path = cachePath! + "/\(file)"
//取出文件属性
do {
let floder = try FileManager.default.attributesOfItem(atPath: path)
for (key, fileSize) in floder {
//累加
if key == FileAttributeKey.size {
size += (fileSize as AnyObject).integerValue
}
}
} catch {
print("出错了!")
}
}
let totalSize = Double(size) / 1024.0 / 1024.0
return String(format: "%.1fM", totalSize)
}
2.清除缓存
//删除缓存
func clearCache() {
MBProgressHUDSwift.showLoading()
//cache文件夹
let cachePath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first
//文件夹下所有文件
let files = FileManager.default.subpaths(atPath: cachePath!)!
//遍历删除
for file in files {
//文件名
let path = cachePath! + "/\(file)"
//存在就删除
if FileManager.default.fileExists(atPath: path) {
do {
try FileManager.default.removeItem(atPath: path)
} catch {
print("出错了!")
}
}
}
MBProgressHUDSwift.dismiss()
cacheSize = getCacheSize()
}
另附SDWebImage清除缓存
//获取图片缓存大小
let sdCacheSize = SDImageCache.shared().getSize()
//清理缓存
SDImageCache.shared().clearDisk {
}