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

本文介绍了如何使用SDWebImage库来解决UIImageView中头像不显示的问题,通过设置sd_setImageWithURL加载网络图片并使用placeholder作为占位图。同时,文章还探讨了如何使用SDWebImage清理缓存以及计算缓存大小,提供了不同单位间的转换展示方式。
摘要由CSDN通过智能技术生成

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];
        }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值