清理缓存
主要是删除/Caches目录下的内容.如果其他路径上的文件可以删除也可以包含进来.
iOS 文件系统也得了解一下
iOS 文件系统
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| + (NSUInteger)getCachesSize { NSUInteger size = 0; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSDirectoryEnumerator *fileEnumerator = [fileManager enumeratorAtPath:cachePath]; for (NSString *fileName in fileEnumerator) { if ([fileName isEqualToString:@"Snapshots"]) { continue; } NSString *filePath = [cachePath stringByAppendingPathComponent:fileName]; NSDictionary *attrs = [fileManager attributesOfItemAtPath:filePath error:nil];
size += [attrs fileSize]; } return size; }
+ (void)clearCachesWithCompletionHandler:(void(^)(void))completionHandler { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSFileManager *fileManager = [NSFileManager defaultManager]; NSString * cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSDirectoryEnumerator *fileEnumerator = [fileManager enumeratorAtPath:cachePath]; for (NSString *fileName in fileEnumerator) { if ([fileName isEqualToString:@"Snapshots"]) { continue; } NSString *filePath = [cachePath stringByAppendingPathComponent:fileName]; BOOL removeRs = [fileManager removeItemAtPath:filePath error:nil]; if (!removeRs) { NSDictionary *attrs = [fileManager attributesOfItemAtPath:filePath error:nil]; DLog(@"移除文件失败:fileType:%@, fileName:%@", [attrs objectForKey:NSFileType], fileName); } } dispatch_async(dispatch_get_main_queue(), ^{ if (completionHandler) { completionHandler(); } }); }); }
|
/Library/Caches/Snapshots/com.AppName/UIApplicationAutomaticSnapshotDefault-LandscapeLeft.png
“/Snapshots”该目录保存的是按下home键时系统对当前屏幕生成的一张快照.
参考:IOS BACKGROUND SCREEN CACHING
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError * _Nullable *)error;
会移除文件或目录