iOS学习笔记-124.多线程23——多图下载3_第三种实现方式(使用SDWebImage)

多线程23——多图下载3_第三种实现方式(使用SDWebImage)

一、说明

前面的例子中,我们已经是实现了我们想要的功能。现在我们使用第三方框架 SDWebImage 来实现一把。这会变得很简单的,我们使用到的方法是在这个框架中的UIImageView+WebCache.h分类中。我们使用的方法如下

/*
 第一个参数:下载图片的url地址
 第二个参数:占位图片
 第三个参数:progress 进度回调
    receivedSize:已经下载的数据大小
    expectedSize:要下载图片的总大小
 第四个参数:
    image:要下载的图片
    error:错误信息
    cacheType:缓存类型
    imageURL:图片url
 */
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;

SDWebImage 下载地址 https://github.com/rs/SDWebImage


二、代码

//
//  ViewController.m
//  03_UIview86多线程_多图下载
//
//  Created by 杞文明 on 17/9/6.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import "ViewController.h"
#import "QWMAppItem.h"
#import "UIImageView+WebCache.h"

@interface ViewController ()
/** tableView的数据源 */
@property (nonatomic, strong) NSArray *apps;
@end

@implementation ViewController

-(NSArray*)apps{
    if(_apps==nil){
        //字典数据
        NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]];
        //字典数据转模型数据
        NSMutableArray *arrM = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            [arrM addObject:[QWMAppItem appWitdDict:dict]];
        }
        _apps = arrM;
    }
    return _apps;
}

/*组数*/
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

/*每组的行数*/
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.apps.count;
}

/*每个cell*/
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //获取数据
    QWMAppItem *appItem = self.apps[indexPath.row];

    cell.textLabel.text = appItem.name;
    cell.detailTextLabel.text = appItem.download;

    [self handle3:cell withItem:appItem];

    return cell;
}

/*图片的第三种种处理方式,使用SDWebImage*/
-(UITableViewCell*)handle3:(UITableViewCell*)cell withItem:(QWMAppItem*)appItem{

  /*
     第一个参数:下载图片的url地址
     第二个参数:占位图片
     第三个参数:progress 进度回调
              receivedSize:已经下载的数据大小
              expectedSize:要下载图片的总大小
     第四个参数:
            image:要下载的图片
             error:错误信息
             cacheType:缓存类型
             imageURL:图片url
     */
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:appItem.icon] placeholderImage:[UIImage imageNamed:@"image1"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        NSLog(@"%f",1.0 * receivedSize / expectedSize);

    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        NSLog(@"%zd",cacheType);
    }];
    return cell;
}


@end

三、图示

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值