From bbcd4beddbe7d98a65fc604afc9246c2004e4eba Mon Sep 17 00:00:00 2001 From: kevinzhow Date: Sun, 1 Nov 2015 05:22:20 +0800 Subject: [PATCH] Add kingfisher delete disk cache --- Yep/Configs/YepHelper.swift | 27 +++++++++++++++++++ .../Profile/ProfileViewController.swift | 12 +++++++++ 2 files changed, 39 insertions(+) diff --git a/Yep/Configs/YepHelper.swift b/Yep/Configs/YepHelper.swift index bfd46e5f..38e0a3fc 100644 --- a/Yep/Configs/YepHelper.swift +++ b/Yep/Configs/YepHelper.swift @@ -10,6 +10,8 @@ import Foundation import RealmSwift import Navi + + typealias CancelableTask = (cancel: Bool) -> Void extension String { @@ -87,3 +89,28 @@ func isOperatingSystemAtLeastMajorVersion(majorVersion: Int) -> Bool { return NSProcessInfo().isOperatingSystemAtLeastVersion(NSOperatingSystemVersion(majorVersion: majorVersion, minorVersion: 0, patchVersion: 0)) } +extension String { + func stringByAppendingPathComponent(path: String) -> String { + return (self as NSString).stringByAppendingPathComponent(path) + } +} + + +func cleanDiskCacheFolder() { + + let folderPath = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0] + let fileMgr = NSFileManager.defaultManager() + + guard let fileArray = try? fileMgr.contentsOfDirectoryAtPath(folderPath) else { + return + } + + for filename in fileArray { + do { + try fileMgr.removeItemAtPath(folderPath.stringByAppendingPathComponent(filename)) + } catch { + print(" clean error ") + } + + } +} \ No newline at end of file diff --git a/Yep/ViewControllers/Profile/ProfileViewController.swift b/Yep/ViewControllers/Profile/ProfileViewController.swift index 42f42f4f..29513f35 100644 --- a/Yep/ViewControllers/Profile/ProfileViewController.swift +++ b/Yep/ViewControllers/Profile/ProfileViewController.swift @@ -12,6 +12,7 @@ import MonkeyKing import Navi import Crashlytics import SafariServices +import Kingfisher let profileAvatarAspectRatio: CGFloat = 12.0 / 16.0 @@ -499,6 +500,17 @@ class ProfileViewController: UIViewController { syncMyInfoAndDoFurtherAction { } + + Kingfisher.ImageCache(name: "default").calculateDiskCacheSizeWithCompletionHandler({ (size) -> () in + let cacheSize = Double(size)/1000000 + + println(String(format: "%.2f MB", cacheSize)) + + if cacheSize > 300 { + Kingfisher.ImageCache.defaultCache.clearDiskCache() + } + + }) title = NSLocalizedString("Profile", comment: "")