第一步:
Snip20161031_5.png
第二步:
Snip20161031_6.png
第三步:
Snip20161031_7.png
#import <Foundation/Foundation.h>
@interface NSString (LYWExtension)
- (unsigned long long)fileSize;
@end
第四步:
Snip20161031_14.png
#import "NSString+LYWExtension.h"
@implementation NSString (LYWExtension)
- (unsigned long long)fileSize
{
// 總大小
unsigned long long size = 0;
// 文件管理者
NSFileManager *mgr = [NSFileManager defaultManager];
// 是否為文件夾
BOOL isDirectory = NO;
// 路徑是否存在
BOOL exists = [mgr fileExistsAtPath:self isDirectory:&isDirectory];
if (!exists) return size;
if (isDirectory) { // 文件夾
// 獲得文件夾的大小 == 獲得文件夾中所有文件的總大小
NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self];
for (NSString *subpath in enumerator) {
// 全路徑
NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
// 累加文件大小
size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize;
}
} else { // 文件
size = [mgr attributesOfItemAtPath:self error:nil].fileSize;
}
return size;
}
@end
初步簡單使用
Snip20161031_13.png
效果:
regi.gif
我們打開沙河查看文件大小,對(duì)比。
Snip20161031_15.png
下面我們就讓它顯示成多少M(fèi) 多少G
Snip20161031_17.png
Snip20161031_18.png
清除緩存
Snip20161031_19.png
代碼附上
#import "SsrtextViewController.h"
#import "NSString+LYWExtension.h"
#import "SVProgressHUD.h"
#import "SDImageCache.h"
@interface SsrtextViewController ()
@property (strong, nonatomic)UILabel *lab;
@end
@implementation SsrtextViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 300, 100)];
_lab.backgroundColor = RandomColor;
_lab.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_lab];
_lab.text = @"清除緩存(正在計(jì)算緩存大小...)";
// 在子線程計(jì)算緩存大小
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 獲得緩存文件夾路徑(這個(gè)是我們自己的緩存文件)
unsigned long long size = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"Custom"].fileSize;
//這個(gè)是SDWebImage緩存的文件
size += [SDImageCache sharedImageCache].getSize;
NSString *sizeText = nil;
if (size >= pow(10, 9)) { // size >= 1GB
sizeText = [NSString stringWithFormat:@"%.2fGB", size / pow(10, 9)];
} else if (size >= pow(10, 6)) { // 1GB > size >= 1MB
sizeText = [NSString stringWithFormat:@"%.2fMB", size / pow(10, 6)];
} else if (size >= pow(10, 3)) { // 1MB > size >= 1KB
sizeText = [NSString stringWithFormat:@"%.2fKB", size / pow(10, 3)];
} else { // 1KB > size
sizeText = [NSString stringWithFormat:@"%zdB", size];
}
//下面也是一種換算文件大小的方式
// if (size >= 1000 * 1000 * 1000) { // size >= 1GB
// sizeText = [NSString stringWithFormat:@"%.2fGB", size / 1000.0 / 1000.0 / 1000.0];
// } else if (size >= 1000 * 1000) { // 1GB > size >= 1MB
// sizeText = [NSString stringWithFormat:@"%.2fMB", size / 1000.0 / 1000.0];
// } else if (size >= 1000) { // 1MB > size >= 1KB
// sizeText = [NSString stringWithFormat:@"%.2fKB", size / 1000.0];
// } else { // 1KB > size
// sizeText = [NSString stringWithFormat:@"%zdB", size];
// }
// 生成文字
NSString *text = [NSString stringWithFormat:@"清除緩存(%@)", sizeText];
// 回到主線程
dispatch_async(dispatch_get_main_queue(), ^{
// 設(shè)置文字
_lab.text = text;
// 添加手勢監(jiān)聽器
[_lab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clearCache)]];
});
});
}
/**
* 清除緩存
*/
- (void)clearCache
{
// 彈出指示器
[SVProgressHUD showInfoWithStatus:@"正在清除緩存..."];
// 刪除SDWebImage的緩存
[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
// 刪除自定義的緩存
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//文件管理者
NSFileManager *mgr = [NSFileManager defaultManager];
//移除該文件夾
[mgr removeItemAtPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"Custom"] error:nil];
//再創(chuàng)建該文件夾
[mgr createDirectoryAtPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"Custom"] withIntermediateDirectories:YES attributes:nil error:nil];
// 所有的緩存都清除完畢
dispatch_async(dispatch_get_main_queue(), ^{
// 隱藏指示器
[SVProgressHUD dismiss];
// 設(shè)置文字
_lab.text = @"清除緩存(0B)";
});
});
}];
}
我們?cè)賮韺?duì)比看一下
Snip20161031_22.png
備注:(如果有需要封裝的文件,可聯(lián)系我,我已經(jīng)教大家封裝了,其實(shí)復(fù)制過去也很快)如果您嫌麻煩那就去github下載Dome:https://github.com/LYWGod/fileSize
如果有不足或者錯(cuò)誤的地方還望各位讀者批評(píng)指正,可以評(píng)論留言,筆者收到后第一時(shí)間回復(fù)。
QQ/微信:2366889552 /lan2018yingwei。
簡書號(hào):凡塵一笑:[簡書]
http://www.lxweimin.com/users/0158007b8d17/latest_articles
感謝各位觀眾老爺?shù)拈喿x,如果覺得筆者寫的還湊合,可以關(guān)注或收藏一下,不定期分享一些好玩的實(shí)用的demo給大家。
文/凡塵一笑(簡書作者)
著作權(quán)歸作者所有,轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),并標(biāo)注“簡書作者”。