[MISSAJJ原创]cell内 通过SDWebImage自定义创建动态菊花加载指示器

最后更新已经放到了github上了

MISSAJJ自己写的一个基于SDWebImage自定义的管理网络图片加载的工具类(普通图片加载,渐现Alpha图片加载,菊花Indicator动画加载)

 

经常在项目里要用到SDWebImage的类来异步加载图片,于是考虑用代码分层的理念和方案,单独写了一个MAImageViewTool工具类用于调用SDWebImage异步加载图片,后期如果项目需要修改就只需要在这个工具类里改写和调试,不用在整个项目里批量寻找再一段一段改写代码了,提高了效率。

在这个类里增加了渐现Alpha图片加载菊花Indicator动画加载的效果,有需要的攻城狮可以用来看看效果。

 

https://github.com/MISSAJJ/MAImageViewTool 

 

 

 

 

/**
*  cell内 普通图片加载

*/

     

    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageToLoad] placeholderImage: [UIImage imageNamed:EMPTY_IMAGE]];

     


 

 

/**
*  cell内 自定义创建图片动态加载指示器

*/

 

__block UIActivityIndicatorView * indicatorPlaceholder;

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageToLoad] placeholderImage:nil options:SDWebImageCacheMemoryOnly   progress:^(NSInteger receivedSize, NSInteger expectedSize) {

//创建指示器:必须放在线程内才不会报错

dispatch_async(dispatch_get_main_queue(), ^{

if(!indicatorPlaceholder){

[cell.imageView addSubview:indicatorPlaceholder = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];

indicatorPlaceholder.center = cell.imageView.center;

[indicatorPlaceholder startAnimating];

}

});

} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

//如果图片未缓存  渐现效果

if (cacheType == SDImageCacheTypeNone) {

cell.imageView.alpha = 0.5;

[UIView animateWithDuration:1.0  animations:^{

cell.imageView.alpha = 1.0;

}];

}

// 消除指示器

这种删除比较彻底
 for (UIView * view in [cell.imageView subviews]) {
           
            if ([view isKindOfClass:[UIActivityIndicatorView class]]) {
            
                [view removeFromSuperview];
            }           
   }
//这种删除方法会有bug,清楚缓存后重载,某些cell上还是会有菊花
//
if (indicatorPlaceholder) { //[indicatorPlaceholder removeFromSuperview]; //indicatorPlaceholder = nil; //} }];

 

转载于:https://www.cnblogs.com/missajj/p/4979640.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值