36.使用SDWebImage解决头像不显示问题

2020-11-24  本文已影响0人  bytebytebyte
1.头像加载


  UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(10+([JGGView imageWidth]+kJGG_GAP)*(i%3),floorf(i/3.0)*([JGGView imageHeight]+kJGG_GAP),[JGGView imageWidth], [JGGView imageHeight])];
            if ([dataSource[i] isKindOfClass:[UIImage class]]) {
                iv.image = dataSource[i];

            }else if ([dataSource[i] isKindOfClass:[NSString class]]){
               [iv sd_setImageWithURL:[NSURL URLWithString:dataSource[i]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
                SDWebImageManager *manager = [SDWebImageManager sharedManager];
                
                [manager downloadImageWithURL:[NSURL URLWithString:dataSource[i]] options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                    
                    
                } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                    
                }];
                
            }


2.清理缓存,计算缓存大小

清理缓存:

 [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];//可有可无


 //利用SDWebImage 来计算缓存大小并显示
        _ImageCacheSize = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_SIZE.width - 100, 0, 80, 50)];
        _ImageCacheSize.textAlignment = 2;//右对齐
        NSInteger tmpSize = [[SDImageCache sharedImageCache] getSize];
        // 1k = 1024, 1m = 1024k
        if (tmpSize < 1024) {// 小于1k
            _ImageCacheSize.text = [NSString stringWithFormat:@"%ldB",(long)tmpSize];
        }else if (tmpSize < 1024 * 1024){// 小于1M
            CGFloat aFloat = tmpSize/1024.00;
            _ImageCacheSize.text =[NSString stringWithFormat:@"%.2fK",aFloat];
        }else if (tmpSize < 1024 * 1024 * 1024){// 小于1G
            CGFloat aFloat = tmpSize/(1024.0 * 1024.00);
            _ImageCacheSize.text = [NSString stringWithFormat:@"%.2fM",aFloat];
        }else{
            CGFloat aFloat = tmpSize/(1024*1024*1024.00);
            _ImageCacheSize.text = [NSString stringWithFormat:@"%.2fG",aFloat];
        }




上一篇 下一篇

猜你喜欢

热点阅读