博客> 清缓存
清缓存
2017-12-12 11:43 评论:0 阅读:706 纯敲嘛

/清除缓存/ //清除缓存点击事件

  • (void)putBufferClicked { __block CGFloat size = [self folderSizeAtPath:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject] + [self folderSizeAtPath:NSTemporaryDirectory()];

    [SDWebImageManager.sharedManager.imageCache calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) { size = size + totalSize; }];

    NSString message = size > 1 ? [NSString stringWithFormat:@"清理缓存 %.2f M", size] : [NSString stringWithFormat:@"清理缓存 %.2f K", size 1024.0];

    LQ_AlertView *alert; alert = [LQ_AlertView alertView:nil des:message type:AKAlertCustom effect:AKAlertEffectFade sureTitle:@"确定" cancelTitle:@"取消"];

    alert.sureClick = ^(LQ_AlertView av){ NSString path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"xxxx.sqlite"];

    [self cleanCaches:path];
    [self cleanCaches:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject];
    [self cleanCaches:NSTemporaryDirectory()];
    
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];
    [[NSURLCache sharedURLCache] removeAllCachedResponses];

    }; alert.cancelClick = ^(LQ_AlertView* av){

    }; [alert show]; }

// 计算目录大小

  • (CGFloat)folderSizeAtPath:(NSString )path{ // 利用NSFileManager实现对文件的管理 NSFileManager manager = [NSFileManager defaultManager]; CGFloat size = 0; if ([manager fileExistsAtPath:path]) { // 获取该目录下的文件,计算其大小 NSArray childrenFile = [manager subpathsAtPath:path]; for (NSString fileName in childrenFile) { NSString *absolutePath = [path stringByAppendingPathComponent:fileName]; size += [manager attributesOfItemAtPath:absolutePath error:nil].fileSize; } // 将大小转化为M return size / 1024.0 / 1024.0; } return 0; }

// 根据路径删除文件

  • (void)cleanCaches:(NSString )cachPath{ NSArray files = [[NSFileManager defaultManager] subpathsAtPath:cachPath]; for (NSString p in files) { NSError error; NSString *path = [cachPath stringByAppendingPathComponent:p]; if ([[NSFileManager defaultManager] fileExistsAtPath:path]) { [[NSFileManager defaultManager] removeItemAtPath:path error:&error]; } }

}

收藏
0
sina weixin mail 回到顶部