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 ...
随机推荐
- 微软Azure云平台Hbase 的使用
In this article What is HBase? Prerequisites Provision HBase clusters using Azure Management portal ...
- ArcGIS 10.3 安装及破解
系统环境:win7 64位操作系统. 一.ArcGIS 10.3包简介 ArcGIS 10.3 下载包含 1. ArcGIS for Desktop ArcGIS for Desktop简介: Ar ...
- Java 基础知识点(必知必会其二)
1.如何将数字输出为每三位逗号分隔的格式,例如“1,234,467”? package com.Gxjun.problem; import java.text.DecimalFormat; impor ...
- 229. Majority Element II -- 找出数组中出现次数超过 ⌊ n/3 ⌋ 次的数
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 201. Bitwise AND of Numbers Range -- 连续整数按位与的和
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- iOS开发几年了,你清楚OC中的这些东西么!!!?
iOS开发几年了,你清楚OC中的这些东西么!!!? 前言 几年前笔者是使用Objective-C进行iOS开发, 不过在两年前Apple发布swift的时候,就开始了swift的学习, 在swift1 ...
- 20145236 《Java程序设计》课程总结
20145236 <Java程序设计>课程总结 一.每周读书笔记链接汇总 第一周读书笔记 第二周读书笔记 第三周读书笔记 第四周读书笔记 第五周读书笔记 第六周读书笔记 第七周读书笔记 第 ...
- 有一种感动叫ACM(记WJMZBMR在成都赛区开幕式上的讲话)
各位选手,各位教练,大家好,我是来自清华大学交叉信息学院的陈立杰,今天很荣幸站在这里代表全体参赛选手发言.对于我来说,这是我第一次正式参加ACM的比赛.不过我跟ACM之间的缘分,大概在很早的时候就已经 ...
- 项目三(集团官网)——总结(1) cookie
最近十几天一直在忙着做集团官方网站的工作,从刚开始紧张的筹备一直到现在初步成型,今天才有时间特地来记录一下自己在这个项目中的收获. 先来说一说今天遇到的问题吧:关于cookie~ 事情起因是这样的:在 ...
- double int char 数据类型
贴心的limits... 测试代码: #include <iostream> #include <stdio.h> #include <limits> #inclu ...