美文网首页
SDWebImage的使用

SDWebImage的使用

作者: 小苗晓雪 | 来源:发表于2017-09-07 14:14 被阅读8次

    SDWebImage的三个常用方法

    //赋值网络图片:
        NSString * imageString = [NSString stringWithFormat:@"%@%@",baseUrl,shop.shop_image];
        [cell.iconView sd_setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"defaultImage"]];
        
        
        [cell.iconView sd_setImageWithURL:[NSURL URLWithString:imageString] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            NSLog(@"下载完成");
        
        }];
        
        //SDWebImage提供了获取下载进度的方法:
        [cell.iconView sd_setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"defaultImage"] options:SDWebImageRetryFailed | SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
            //获取图片下载进度:
            NSLog(@"%ld" , receivedSize / expectedSize);
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            NSLog(@"下载完成");
            
        }];
    
    #import "ViewController.h"
    #import "Masonry.h"
    #import "ShopCell.h"
    #import "UIImageView+WebCache.h"    //导入SDWebImage
    static NSString * baseUrl = @"http://192.168.XX.XX/images/";  //网络接口地址
    
    @interface ViewController ()
    @property (nonatomic, strong) NSArray * datalist;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [self.tableView registerClass:[ShopCell class] forCellReuseIdentifier:NSStringFromClass([ShopCell class])];
        self.tableView.rowHeight = 100;
    }
    
    
    - (NSArray *)datalist
    {
        if (!_datalist)
        {
            _datalist = [ShopEntity loadShopData];
        }
        return _datalist;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.datalist.count;
    }
    
    
    #pragma mark - <UITableViewDataSource>
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        ShopCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ShopCell class])];
        ShopEntity * shop = self.datalist[indexPath.row];
        //赋值主标题:
        cell.nameLabel.text = shop.shop_name;
        //赋值副标题:
        cell.descLabel.text = shop.shop_desc;
        
        //赋值网络图片:
        NSString * imageString = [NSString stringWithFormat:@"%@%@",baseUrl,shop.shop_image];
        [cell.iconView sd_setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"defaultImage"]];
        
        
        [cell.iconView sd_setImageWithURL:[NSURL URLWithString:imageString] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            NSLog(@"下载完成");
        
        }];
        
        //SDWebImage提供了获取下载进度的方法:
        [cell.iconView sd_setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"defaultImage"] options:SDWebImageRetryFailed | SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
            //获取图片下载进度:
            NSLog(@"%ld" , receivedSize / expectedSize);
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            NSLog(@"下载完成");
            
        }];
        
        return cell;
        
    }
    
    
    #pragma mark - <UITableViewDelegate>
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
    //options 图片缓存策略
    /*
     options所有选项:
     //失败后重试
     SDWebImageRetryFailed = 1 << 0,
           
     //UI交互期间开始下载,导致延迟下载比如UIScrollView减速。
     SDWebImageLowPriority = 1 << 1,
           
     //只进行内存缓存
     SDWebImageCacheMemoryOnly = 1 << 2,
           
     //这个标志可以渐进式下载,显示的图像是逐步在下载
     SDWebImageProgressiveDownload = 1 << 3,
           
     //刷新缓存
     SDWebImageRefreshCached = 1 << 4,
           
     //后台下载
     SDWebImageContinueInBackground = 1 << 5,
           
     //优先下载
     SDWebImageHighPriority = 1 << 8,
           
     //延迟占位符
     SDWebImageDelayPlaceholder = 1 << 9,
           
     //改变动画形象
     SDWebImageTransformAnimatedImage = 1 << 10,
     
     */
    
    @end
    
    

    愿编程让这个世界更美好

    相关文章

      网友评论

          本文标题:SDWebImage的使用

          本文链接:https://www.haomeiwen.com/subject/xudijxtx.html