SDWebImage清除快取

weixin_33890499發表於2016-03-08
1. 計算圖片快取大小
 - (void)imageCacheSize:(NSInteger) accuracy
 {
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        CGFloat imageSize = ([[SDImageCache sharedImageCache] getSize] / 1024.0f) / 1024.0f;
        NSString *imageSizeString = [NSString stringWithFormat:@"%f",imageSize];
        NSRange dotRange = [imageSizeString rangeOfString:@"."];
        NSInteger lastPosition = dotRange.location + accuracy;
        imageSizeString = [imageSizeString substringToIndex:lastPosition];
        dispatch_async(dispatch_get_main_queue(), ^{
          self.cacheLabel.text = [NSString stringWithFormat:@"%@ M",imageSizeString];
        });
    });
 }
2. 清除快取
 - (void)goToClearCache
 {
     [NSThread detachNewThreadSelector:@selector(clearCache) toTarget:self withObject:nil];
     //loading
     [self performSelector:@selector(clearCacheFinished) withObject:nil afterDelay:1];
 }
 - (void)clearCache
 {
      [[SDImageCache sharedImageCache] clearDisk];
      [[SDImageCache sharedImageCache] clearMemory];
 }
 - (void)clearCacheFinished
 {
     //清除成功
     //快取大小清空
     self.cacheLabel.text = @"0 M";
 }

相關文章