2016 - 1- 19 利用多线程优化从网上加载图片的Demo
//
// 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.第一次打开从网络上加载时:

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

2016 - 1- 19 利用多线程优化从网上加载图片的Demo的更多相关文章
- (Android图片内存优化)Picasso加载图片 教程。。详细版
Picasso 是 Android 上一个强大的图片下载和缓存库. 示例代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Picasso.with( ...
- Listview 异步加载图片之优化篇(有图有码有解释)
在APP应用中,listview的异步加载图片方式能够带来很好的用户体验,同时也是考量程序性能的一个重要指标.关于listview的异步加载,网上其实很多示例了,中心思想都差不多,不过很多版本或是有b ...
- Listview异步加载图片之优化篇
在APP应用中,listview的异步加载图片方式能够带来很好的用户体验,同时也是考量程序性能的一个重要指标.关于listview的异步加载,网上其实很多示例了,中心思想都差不多,不过很多版本或是有b ...
- iOS网络加载图片缓存策略之ASIDownloadCache缓存优化
iOS网络加载图片缓存策略之ASIDownloadCache缓存优化 在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...
- android 网络加载图片,对图片资源进行优化,并且实现内存双缓存 + 磁盘缓存
经常会用到 网络文件 比如查看大图片数据 资源优化的问题,当然用开源的项目 Android-Universal-Image-Loader 或者 ignition 都是个很好的选择. 在这里把原来 ...
- requirejs:性能优化-及早并行加载
为了提高页面的性能,通常情况下,我们希望资源尽可能地早地并行加载.这里有两个要点,首先是尽早,其次是并行. 通过data-main方式加载要尽可能地避免,因为它让requirejs.业务代码不必要地串 ...
- android优化从网络中加载图片速度。。
从网络中加载图片主要要注意两个方面的问题: 1.内存管理:图片占的内存很大,假如图片数量多,很容易让系统抛出out of memory的异常. 同时我们也要注意不同android版本中内存管理的区别. ...
- 多线程异步加载图片async_pictures
异步加载图片 目标:在表格中异步加载网络图片 目的: 模拟 SDWebImage 基本功能实现 理解 SDWebImage 的底层实现机制 SDWebImage 是非常著名的网络图片处理框架,目前国内 ...
- Vue优化首屏加载
背景: 使用vue + iview搭建的一个后台管理系统,路由已经用了懒加载,加载登陆页面,居然还是需要18S左右,刚到一个新公司,项目经理很委婉的说,看看能不能优化了一下.然后就开始了网上一大堆'v ...
随机推荐
- android:layout_weight的真实含义
首先声明只有在Linearlayout中,该属性才有效.之所以android:layout_weight会引起争议, 是因为在设置该属性的同时,设置android:layout_width为wrap_ ...
- iOS响应式编程:ReactiveCocoa vs RxSwift 选谁好
转载: iOS响应式编程:ReactiveCocoa vs RxSwift 选谁好 内容来自stack overflow的一个回答:ReactiveCocoa vs RxSwift – pros an ...
- kmeans算法原理以及实践操作(多种k值确定以及如何选取初始点方法)
kmeans一般在数据分析前期使用,选取适当的k,将数据聚类后,然后研究不同聚类下数据的特点. 算法原理: (1) 随机选取k个中心点: (2) 在第j次迭代中,对于每个样本点,选取最近的中心点,归为 ...
- 《Java程序设计》第三周学习总结
20145224-陈颢文 <Java程序设计>第三周学习总结 教材学习内容总结 一.定义类: ·类定义时使用class关键字,要对类中变量(值域成员/对象数据成员)行类型声明. class ...
- python中Url链接编码处理(urlencode,urldecode)
做完了flask-web应用,这几天想用爬虫做个好玩的电影链接整合器,平时找电影都是在dytt或者dy2018之类的网站,在用dytt搜索电影<美国队长时>,发现他的搜索链接是这样的:ht ...
- bug:C#线程间操作无效: 从不是创建控件" XX" 的线程访问它
今天遇到这个问题,百度了下,把解决的方法总结出来.我们在ui线程创建的子线程操作ui控件时,系统提示错误详细信息为:线程间操作无效: 从不是创建控件“XXX”的线程访问它. 就我知道的有三种方法,先看 ...
- 小例子(一)、MD5加密
一个MD5加密的小案例 代码如下: using System; using System.Text; using CCWin; using System.Security.Cryptography; ...
- 笔记8:winfrom连接数据库DBHelp
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- Java 集合系列 09 HashMap详细介绍(源码解析)和使用示例
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...