• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Miss琴瑟静听 ====IOS开发博客
琴瑟静听,岁月无恙
博客园    首页    新随笔    联系   管理    订阅  订阅

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

 

posted on 2015-11-20 02:25  Miss琴瑟静听  阅读(567)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3