首页 技术 正文
技术 2022年11月9日
0 收藏 620 点赞 4,541 浏览 3325 个字
//
// ZZTableViewController.m
// 多图片下载
//
// Created by Mac on 16/1/19.
// Copyright © 2016年 Mac. All rights reserved.
//#import "ZZTableViewController.h"
#import "ZZApp.h"
@interface ZZTableViewController ()
// 用来存放模型
@property (nonatomic, strong) NSArray *apps;@property (nonatomic, strong) NSMutableDictionary *iconDic;// 用于存放下载操作
@property (nonatomic, strong) NSMutableDictionary *operations;@property (nonatomic, strong) NSOperationQueue *queue;@end@implementation ZZTableViewController- (NSOperationQueue *)queue
{
if (!_queue) {
_queue = [[NSOperationQueue alloc] init]; }
return _queue;
}- (NSMutableDictionary *)operations
{
if (!_operations) {
_operations = [NSMutableDictionary dictionary]; }
return _operations;
}- (NSMutableDictionary *)iconDic{
if (!_iconDic) {
_iconDic = [NSMutableDictionary dictionary];
}
return _iconDic;
}- (NSArray *)apps
{
if (!_apps) {
NSMutableArray *tmpArray = [NSMutableArray array]; NSString *path = [[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]; NSArray *dicArray = [NSArray arrayWithContentsOfFile:path]; for (NSDictionary *dic in dicArray) {
ZZApp *app = [ZZApp appWithdic:dic];
[tmpArray addObject:app];
}
_apps = tmpArray;
}
return _apps;
}- (void)viewDidLoad {
[super viewDidLoad];}- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
return ;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
return self.apps.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"app";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
ZZApp *app = self.apps[indexPath.row];
cell.textLabel.text = app.name;
cell.detailTextLabel.text = app.download;
UIImage *image = self.iconDic[app.icon]; if (image) {// 先判断内存中有没有图片
cell.imageView.image = image;
}else{
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// 获得文件名
NSString *fileName = [app.icon lastPathComponent];
// 计算出文件全路径
NSString *file = [cachePath stringByAppendingString:fileName];
// 加载沙盒数据
NSData *data = [NSData dataWithContentsOfFile:file];
// 判断沙盒里有没有数据 if (data) {// 沙盒中有数据 cell.imageView.image = [UIImage imageWithData:data];
// cell.imageView.image =
// 把数据加载到内存里
[self.iconDic setObject:cell.imageView.image forKey:app.icon]; }else{// 下载图片
// cell.imageView.image = [UIImage imageNamed:@"屏幕快照 2016-01-19 下午9.54.43"];
// 添加下载操作到字典里
NSOperation *operation = [self.operations objectForKey:app.icon];
if (!operation) {
operation = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:app.icon];
NSData *data = [NSData dataWithContentsOfURL:url];
if (!data) {// 如果下载失败,移除字典里的操作
[self.operations removeObjectForKey:app.icon];
return ;
} // 回到主线程显示图片
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// cell.imageView.image = [UIImage imageWithData:data];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}]; // 放到沙盒里
[data writeToFile:file atomically:YES]; // 将图片存到缓存中
[self.iconDic setObject:[UIImage imageWithData:data] forKey:app.icon]; // 下载完成,移除操作
[self.operations removeObjectForKey:app.icon]; }];
// 添加到队列里面去
[self.queue addOperation:operation];
// 添加到操作字典里去
[self.operations setObject:operation forKey:app.icon];
}
}
}
return cell;
}@end

效果:

1.第一次打开从网络上加载时:

2016 – 1- 19 利用多线程优化从网上加载图片的Demo

2.第二次打开时,从沙盒中加载:

2016 – 1- 19 利用多线程优化从网上加载图片的Demo

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289