iOS随笔小记

iOS随笔小记--计算缓存

2017-06-23  本文已影响2人  七一小月
计算缓存的办法
SDImageCache *sd=[[SDImageCache alloc]init];

NSInteger a=[sd getSize];

CGFloat size = a/1024.0/1024.0;

self.clearLabel.text = [NSString stringWithFormat:@"当前缓存%.2fKB",size];
清除缓存
-(void)action:(UITapGestureRecognizer *)tap {

//清理缓存

SDImageCache *sd=[[SDImageCache alloc]init];

[sd clearDisk];

self.clearLabel.text = @"当前缓存0KB";

}
清除缓存步骤与显示弹框

单例(一个应用程序只有一个对象)

SDImageCache *sdImageCache = [SDImageCache sharedImageCache];

NSString *str = [NSString stringWithFormat:@"缓存大小%.2fM.是否清除缓存?",     [sdImageCache checkTmpSize]];

NSString *cancelStr = @"取消";

    if ([sdImageCache checkTmpSize] == 0) {

    str = @"您还没有缓存, 不需要清理哦!";

    cancelStr = nil;

}

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:cancelStr otherButtonTitles:@"确定", nil];

// 这里执行协议方法的对象是这个视图控制器对象(协议方法是给别人用的)

alertView.delegate = self;

[alertView show];

[alertView release];

// alertView协议方法执行清除缓存的方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;{

    if (buttonIndex == 1) {

        [[SDImageCache sharedImageCache] clearDisk];
    }
}

// 计算缓存文件大小方法的实现

- (float)checkTmpSize {

    float totalSize = 0;

    NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:_diskCachePath];

    for (NSString *fileName in fileEnumerator) {

    NSString *filePath = [_diskCachePath stringByAppendingPathComponent:fileName];

    NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];

    unsigned long long length = [attrs fileSize];

    totalSize += length / 1024.0 / 1024.0;

        } // NSLog(@"tmp size is %.2f",totalSize);
   
     return totalSize;

}
上一篇下一篇

猜你喜欢

热点阅读