Giter Site home page Giter Site logo

beichenglangzi / szkcleancache Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 18811314750/szkcleancache

0.0 2.0 0.0 20 KB

iOS开发-清理缓存,将计算缓存,清理缓存封装成类方法,并将清理结果利用block方法实现返回,一键调用,轻松实现

Objective-C 100.00%

szkcleancache's Introduction

SZKCleanCache

iOS开发-清理缓存,将计算缓存,清理缓存封装成类方法,并将清理结果利用block方法实现返回,一键调用,轻松实现

SZKCleanCache 详细介绍:http://www.jianshu.com/p/5ebe4f21c486

封装的SZKCleanCache.h中

#import <Foundation/Foundation.h>

typedef void(^cleanCacheBlock)();

@interface SZKCleanCache : NSObject
/**
 *  清理缓存
 */
+(void)cleanCache:(cleanCacheBlock)block;
/**
 *  整个缓存目录的大小
 */
+(float)folderSizeAtPath;

@end

SZKCleanCache.m中实现方法:

/**
 *  计算单个文件大小
 */
+(long long)fileSizeAtPath:(NSString *)filePath{

    NSFileManager *manager = [NSFileManager defaultManager];

    if ([manager fileExistsAtPath :filePath]){

        return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize];
    }
    return 0 ;

}
/**
 *  计算整个目录大小
 */
+(float)folderSizeAtPath
{
    NSString *folderPath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];

    NSFileManager * manager=[NSFileManager defaultManager ];
    if (![manager fileExistsAtPath :folderPath]) {
        return 0 ;
    }
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ];
    NSString * fileName;
    long long folderSize = 0 ;
    while ((fileName = [childFilesEnumerator nextObject ]) != nil ){
        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        folderSize += [ self fileSizeAtPath :fileAbsolutePath];
    }

    return folderSize/( 1024.0 * 1024.0 );
}

/**
 *  清理缓存
 */
+(void)cleanCache:(cleanCacheBlock)block
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //文件路径
        NSString *directoryPath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];

        NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];

        for (NSString *subPath in subpaths) {
            NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
            [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
        }
        //返回主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            block();
        });
    });

}

SZKCleanCache 详细介绍:http://www.jianshu.com/p/5ebe4f21c486

szkcleancache's People

Contributors

18811314750 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.