多线程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