复习下线程的基础知识, 这里主要是参考文顶顶多线程篇复习写的。
一、cell下载图片思路 – 无沙盒(内存)缓存
主要解决下列问题
1、下载操作放在子线程不会卡UI。
2、使用operations字典解决重复下载问题,每个cell对应一个Operation
3、从字典中移除下载操作 (防止operations越来越大,保证下载失败后,能重新下载)
4、存放图片到images字典中,tableView要刷新对应行数的cell,防止正在下载时拖动tableView将当前的Cell放进缓存池,然后又将这个cell放在新的行数,导致下载完成显示对应的图片的位置不对。
5、使用占位图可以防止未下载的cell重用到其他行已经下载的的图片。
6、拖拽tableView时暂停下载操作,让tableView流程滑动。
7、接收到内存警告时移除下载操作和图片字典,待内存小时重新下载。
APPModel文件
#import <Foundation/Foundation.h>
@interface SSAPPModel : NSObject
@property(nonatomic, copy) NSString *name;
@property(nonatomic, copy) NSString *icon;
@property(nonatomic, copy) NSString *download;
+ (instancetype)appModelWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end
@implementation SSAPPModel
- (instancetype)initWithDict:(NSDictionary *)dict {
if (self = [super init]) {//KVC
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)appModelWithDict:(NSDictionary *)dict {
return [[self alloc] initWithDict:dict];
}
@end
#import "SSViewController.h"
#import "SSAPPModel.h"
@interface SSViewController ()<UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
///数据模型
@property (nonatomic, strong) NSArray<SSAPPModel *> *apps;
///存放所有下载操作的队列
@property (nonatomic, strong) NSOperationQueue *queue;
///存放所有的下载操作(url是key,operation对象是value)
@property (nonatomic, strong) NSMutableDictionary *operations;
///存放所有下载完的图片
@property (nonatomic, strong) NSMutableDictionary *images;
@end
@implementation SSViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self configData];
[self configSubViews];
}
//初始化数据
- (void)configData {
// 1.加载plist
NSString *file = [[NSBundle mainBundle] pathForResource:@"apps" ofType:@"plist"];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:file];
// 2.字典 --> 模型
NSMutableArray *appArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
SSAPPModel *app = [SSAPPModel appModelWithDict:dict];
[appArray addObject:app];
}
_apps = appArray;
_queue = [[NSOperationQueue alloc] init];
_operations = [NSMutableDictionary dictionary];
_images = [NSMutableDictionary dictionary];
}
//初始化View
- (void)configSubViews {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
// 取出模型
SSAPPModel *app = self.apps[indexPath.row];
// 设置基本信息
cell.textLabel.text = app.name;
cell.detailTextLabel.text = app.download;
// 先从images缓存中取出图片url对应的UIImage
UIImage *image = self.images[app.icon];
if (image) { // 说明图片已经下载成功过(成功缓存)
cell.imageView.image = image;
} else { // 说明图片并未下载成功过(并未缓存过)
// 显示占位图片
cell.imageView.image = [UIImage imageNamed:@"placeholder"];
// 下载图片
[self download:app.icon indexPath:indexPath];
}
return cell;
}
- (void)download:(NSString *)imageUrl indexPath:(NSIndexPath *)indexPath {
// 取出当前图片url对应的下载操作(operation对象)
NSBlockOperation *operation = self.operations[imageUrl];
if (operation) {// 已经有了operation 不需要创建
NSLog(@"已经有了operation, return");
return;
}
// 创建操作,下载图片
__weak typeof(self) weakSelf = self;
operation = [NSBlockOperation blockOperationWithBlock:^{
// [NSThread sleepForTimeInterval:1.0];//模拟下载慢操作
NSURL *url = [NSURL URLWithString:imageUrl];
NSData *data = [NSData dataWithContentsOfURL:url]; // 下载
UIImage *image = [UIImage imageWithData:data]; // NSData -> UIImage
// 回到主线程
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// 存放图片到字典中
if (image) {
weakSelf.images[imageUrl] = image;
}
// 从字典中移除下载操作 (防止operations越来越大,保证下载失败后,能重新下载)
[weakSelf.operations removeObjectForKey:imageUrl];
// 刷新表格
[weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}];
}];
NSLog(@"imageUrl == %@", imageUrl);
// 添加操作到队列中
[self.queue addOperation:operation];
// 添加到字典中 (这句代码为了解决重复下载)
self.operations[imageUrl] = operation;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.apps.count;
}
#pragma mark - 拖动暂停恢复下载
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
// 暂停下载
[self.queue setSuspended:YES];
}
//当用户停止拖拽表格时调用
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
// 恢复下载
[self.queue setSuspended:NO];
}
#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// 移除所有的下载操作缓存
[self.queue cancelAllOperations];
[self.operations removeAllObjects];
// 移除所有的图片缓存
[self.images removeAllObjects];
}
@end
二、cell下载图片思路 – 有沙盒缓存
应用了沙盒缓存,解决每次打开app都会下载问题。
代码示例
#import "SSViewController.h"
#import "SSAPPModel.h"
@interface SSViewController ()<UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
///数据模型
@property (nonatomic, strong) NSArray<SSAPPModel *> *apps;
///存放所有下载操作的队列
@property (nonatomic, strong) NSOperationQueue *queue;
///存放所有的下载操作(url是key,operation对象是value)
@property (nonatomic, strong) NSMutableDictionary *operations;
///存放所有下载完的图片
@property (nonatomic, strong) NSMutableDictionary *images;
@end
@implementation SSViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self configData];
[self configSubViews];
}
//初始化数据
- (void)configData {
// 1.加载plist
NSString *file = [[NSBundle mainBundle] pathForResource:@"apps" ofType:@"plist"];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:file];
// 2.字典 --> 模型
NSMutableArray *appArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
SSAPPModel *app = [SSAPPModel appModelWithDict:dict];
[appArray addObject:app];
}
_apps = appArray;
_queue = [[NSOperationQueue alloc] init];
_operations = [NSMutableDictionary dictionary];
_images = [NSMutableDictionary dictionary];
}
//初始化View
- (void)configSubViews {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
// 取出模型
SSAPPModel *app = self.apps[indexPath.row];
// 设置基本信息
cell.textLabel.text = app.name;
cell.detailTextLabel.text = app.download;
// 先从images缓存中取出图片url对应的UIImage
UIImage *image = self.images[app.icon];
if (image) { // 说明图片已经下载成功过(成功缓存)
cell.imageView.image = image;
} else { // 说明图片并未下载成功过(并未缓存过)
NSString *file = [self filePathWithImageUrl:app.icon];
// 先从沙盒中取出图片
NSData *data = [NSData dataWithContentsOfFile:file];
if (data) { // 沙盒中存在这个文件
cell.imageView.image = [UIImage imageWithData:data];
} else {// 沙盒中不存在这个文件
// 显示占位图片
cell.imageView.image = [UIImage imageNamed:@"placeholder"];
// 下载图片
[self download:app.icon indexPath:indexPath];
}
}
return cell;
}
- (void)download:(NSString *)imageUrl indexPath:(NSIndexPath *)indexPath {
// 取出当前图片url对应的下载操作(operation对象)
NSBlockOperation *operation = self.operations[imageUrl];
if (operation) {// 已经有了operation 不需要创建
NSLog(@"已经有了operation, return");
return;
}
// 创建操作,下载图片
__weak typeof(self) weakSelf = self;
operation = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:3.0];//模拟下载慢操作
NSURL *url = [NSURL URLWithString:imageUrl];
NSData *data = [NSData dataWithContentsOfURL:url]; // 下载
UIImage *image = [UIImage imageWithData:data]; // NSData -> UIImage
// 回到主线程
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// 存放图片到字典中
if (image) {
weakSelf.images[imageUrl] = image;
}
//将图片存入沙盒中
// UIImage --> NSData --> File(文件)
NSData *data = UIImagePNGRepresentation(image);
NSString *file = [self filePathWithImageUrl:imageUrl];
[data writeToFile:file atomically:YES];
// 从字典中移除下载操作 (防止operations越来越大,保证下载失败后,能重新下载)
[weakSelf.operations removeObjectForKey:imageUrl];
// 刷新表格
[weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}];
}];
NSLog(@"imageUrl == %@", imageUrl);
// 添加操作到队列中
[self.queue addOperation:operation];
// 添加到字典中 (这句代码为了解决重复下载)
self.operations[imageUrl] = operation;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.apps.count;
}
#pragma mark - 拖动暂停恢复下载
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
// 暂停下载
[self.queue setSuspended:YES];
}
//当用户停止拖拽表格时调用
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
// 恢复下载
[self.queue setSuspended:NO];
}
#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// 移除所有的下载操作缓存
[self.queue cancelAllOperations];
[self.operations removeAllObjects];
// 移除所有的图片缓存
[self.images removeAllObjects];
}
#pragma mark - 沙盒图片路径
- (NSString *)filePathWithImageUrl:(NSString *)imageUrl {
NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [cacheDir stringByAppendingPathComponent:[imageUrl lastPathComponent]];
return path;
}
@end
沙盒中图片
三、自定义NSOperation
自定义NSOperation的步骤
- 重写
- (void)main
方法,在里面实现想执行的任务 - 重写
- (void)main
方法的注意点 - 自己创建自动释放池(因为如果是异步操作,无法访问主线程的自动释放池)
- 经常通过
- (BOOL)isCancelled
方法检测操作是否被取消,对取消做出响应
代码示例
#import <UIKit/UIKit.h>
@class SSDownloadOperation;
@protocol SSDownloadOperationDelegate <NSObject>
@optional
- (void)downloadOperation:(SSDownloadOperation *)operation didFinishDownload:(UIImage *)image;
@end
@interface SSDownloadOperation : NSOperation
@property (nonatomic, copy) NSString *imageUrl;
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, weak) id<SSDownloadOperationDelegate> delegate;
@end
#import "SSDownloadOperation.h"
@implementation SSDownloadOperation
- (void)main {
@autoreleasepool {
if (self.isCancelled) {
return;
}
NSURL *url = [NSURL URLWithString:self.imageUrl];
NSData *data = [NSData dataWithContentsOfURL:url]; // 下载
UIImage *image = [UIImage imageWithData:data]; // NSData -> UIImage
if (self.isCancelled) return;
// 回到主线程
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
if ([self.delegate respondsToSelector:@selector(downloadOperation:didFinishDownload:)]) {
[self.delegate downloadOperation:self didFinishDownload:image];
}
}];
}
}
@end
#import "SSViewController.h"
#import "SSAPPModel.h"
#import "SSDownloadOperation.h"
@interface SSViewController ()<UITableViewDataSource, SSDownloadOperationDelegate>
@property (nonatomic, strong) UITableView *tableView;
///数据模型
@property (nonatomic, strong) NSArray<SSAPPModel *> *apps;
///存放所有下载操作的队列
@property (nonatomic, strong) NSOperationQueue *queue;
///存放所有的下载操作(url是key,operation对象是value)
@property (nonatomic, strong) NSMutableDictionary *operations;
///存放所有下载完的图片
@property (nonatomic, strong) NSMutableDictionary *images;
@end
@implementation SSViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self configData];
[self configSubViews];
}
//初始化数据
- (void)configData {
// 1.加载plist
NSString *file = [[NSBundle mainBundle] pathForResource:@"apps" ofType:@"plist"];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:file];
// 2.字典 --> 模型
NSMutableArray *appArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
SSAPPModel *app = [SSAPPModel appModelWithDict:dict];
[appArray addObject:app];
}
_apps = appArray;
_queue = [[NSOperationQueue alloc] init];
_operations = [NSMutableDictionary dictionary];
_images = [NSMutableDictionary dictionary];
}
//初始化View
- (void)configSubViews {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
// 取出模型
SSAPPModel *app = self.apps[indexPath.row];
// 设置基本信息
cell.textLabel.text = app.name;
cell.detailTextLabel.text = app.download;
// 先从images缓存中取出图片url对应的UIImage
UIImage *image = self.images[app.icon];
if (image) { // 说明图片已经下载成功过(成功缓存)
cell.imageView.image = image;
} else { // 说明图片并未下载成功过(并未缓存过)
NSString *file = [self filePathWithImageUrl:app.icon];
// 先从沙盒中取出图片
NSData *data = [NSData dataWithContentsOfFile:file];
if (data) { // 沙盒中存在这个文件
cell.imageView.image = [UIImage imageWithData:data];
} else {// 沙盒中不存在这个文件
// 显示占位图片
cell.imageView.image = [UIImage imageNamed:@"placeholder"];
// 下载图片
[self download:app.icon indexPath:indexPath];
}
}
return cell;
}
- (void)download:(NSString *)imageUrl indexPath:(NSIndexPath *)indexPath {
// 取出当前图片url对应的下载操作(operation对象)
SSDownloadOperation *operation = self.operations[imageUrl];
if (operation) {// 已经有了operation 不需要创建
NSLog(@"已经有了operation, return");
return;
}
// 创建操作,下载图片
operation = [[SSDownloadOperation alloc] init];
operation.imageUrl = imageUrl;
operation.indexPath = indexPath;
// 设置代理
operation.delegate = self;
// 添加操作到队列中
[self.queue addOperation:operation];
// 添加到字典中 (这句代码为了解决重复下载)
self.operations[imageUrl] = operation;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.apps.count;
}
#pragma mark - SSDownloadOperationDelegate下载操作的代理方法
- (void)downloadOperation:(SSDownloadOperation *)operation didFinishDownload:(UIImage *)image {
// 存放图片到字典中
if (image) {
self.images[operation.imageUrl] = image;
//将图片存入沙盒中
NSData *data = UIImagePNGRepresentation(image);
[data writeToFile:[self filePathWithImageUrl:operation.imageUrl] atomically:YES];
}
// 从字典中移除下载操作 (防止operations越来越大,保证下载失败后,能重新下载)
[self.operations removeObjectForKey:operation.imageUrl];
// 刷新表格
[self.tableView reloadRowsAtIndexPaths:@[operation.indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
#pragma mark - 拖动暂停恢复下载
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
// 暂停下载
[self.queue setSuspended:YES];
}
//当用户停止拖拽表格时调用
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
// 恢复下载
[self.queue setSuspended:NO];
}
#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// 移除所有的下载操作缓存
[self.queue cancelAllOperations];
[self.operations removeAllObjects];
// 移除所有的图片缓存
[self.images removeAllObjects];
}
#pragma mark - 沙盒图片路径
- (NSString *)filePathWithImageUrl:(NSString *)imageUrl {
NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [cacheDir stringByAppendingPathComponent:[imageUrl lastPathComponent]];
return path;
}
@end
附件
效果图 placeholder.png<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>name</key>
<string>植物大战僵尸</string>
<key>icon</key>
<string>http://p16.qhimg.com/dr/48_48_/t0125e8d438ae9d2fbb.png</string>
<key>download</key>
<string>10311万</string>
</dict>
<dict>
<key>name</key>
<string>捕鱼达人2</string>
<key>icon</key>
<string>http://p19.qhimg.com/dr/48_48_/t0101e2931181bb540d.png</string>
<key>download</key>
<string>9982万</string>
</dict>
<dict>
<key>name</key>
<string>保卫萝卜</string>
<key>icon</key>
<string>http://p17.qhimg.com/dr/48_48_/t012d281e8ec8e27c06.png</string>
<key>download</key>
<string>8582万</string>
</dict>
<dict>
<key>name</key>
<string>找你妹</string>
<key>icon</key>
<string>http://p18.qhimg.com/dr/48_48_/t0184f949337481f071.png</string>
<key>download</key>
<string>5910万</string>
</dict>
<dict>
<key>name</key>
<string>水果忍者</string>
<key>icon</key>
<string>http://p17.qhimg.com/dr/48_48_/t015f10076f95e27e74.png</string>
<key>download</key>
<string>5082万</string>
</dict>
<dict>
<key>name</key>
<string>鳄鱼小顽皮</string>
<key>icon</key>
<string>http://p16.qhimg.com/dr/48_48_/t01885f5596e1d30172.png</string>
<key>download</key>
<string>3918万</string>
</dict>
<dict>
<key>name</key>
<string>神偷奶爸</string>
<key>icon</key>
<string>http://p19.qhimg.com/dr/48_48_/t0164ad383c622aabef.png</string>
<key>download</key>
<string>3681万</string>
</dict>
<dict>
<key>name</key>
<string>时空猎人</string>
<key>icon</key>
<string>http://p17.qhimg.com/dr/48_48_/t017bc3cfcf3981b197.png</string>
<key>download</key>
<string>3645万</string>
</dict>
<dict>
<key>name</key>
<string>愤怒的小鸟</string>
<key>icon</key>
<string>http://p18.qhimg.com/dr/48_48_/t012fea7312194537c2.png</string>
<key>download</key>
<string>3552万</string>
</dict>
<dict>
<key>name</key>
<string>滑雪大冒险</string>
<key>icon</key>
<string>http://p18.qhimg.com/dr/48_48_/t01e61cbba53fb9eb82.png</string>
<key>download</key>
<string>3487万</string>
</dict>
<dict>
<key>name</key>
<string>爸爸去哪儿</string>
<key>icon</key>
<string>http://p18.qhimg.com/dr/48_48_/t0108c33d3321352682.png</string>
<key>download</key>
<string>3117万</string>
</dict>
<dict>
<key>name</key>
<string>我叫MT </string>
<key>icon</key>
<string>http://p17.qhimg.com/dr/48_48_/t01077fd80ffb5c8740.png</string>
<key>download</key>
<string>2386万</string>
</dict>
<dict>
<key>name</key>
<string>3D终极狂飙</string>
<key>icon</key>
<string>http://p17.qhimg.com/dr/48_48_/t01f55acd4a3ed024eb.png</string>
<key>download</key>
<string>2166万</string>
</dict>
<dict>
<key>name</key>
<string>杀手2</string>
<key>icon</key>
<string>http://p16.qhimg.com/dr/48_48_/t018f89d6e0922f75a1.png</string>
<key>download</key>
<string>1951万</string>
</dict>
<dict>
<key>name</key>
<string>俄罗斯方块</string>
<key>icon</key>
<string>http://p0.qhimg.com/dr/48_48_/t0183a670f1dbff380f.png</string>
<key>download</key>
<string>1290万</string>
</dict>
<dict>
<key>name</key>
<string>刀塔传奇</string>
<key>icon</key>
<string>http://p16.qhimg.com/dr/48_48_/t01c3f62a27c3de7af5.png</string>
<key>download</key>
<string>1249万</string>
</dict>
</array>
</plist>
网友评论