【iOS】网络载入图片缓存与SDWebImage
载入网络图片能够说是网络应用中必备的。假设单纯的去下载图片,而不去做多线程、缓存等技术去优化,载入图片时的效果与用户体验就会非常差。
一、自己实现载入图片的方法
tips:
而要做本地缓存的话,还要自己手动存储网络上下载的图片.
下面代码用NSOperation开异步线程下载图片。当下载完毕时替换占位图片。
//
// XNViewController.m
// 载入网络图片, 普通的用NSOperation来做.
//
// Created by neng on 14-7-7.
// Copyright (c) 2014年 neng. All rights reserved.
// #import "XNViewController.h"
#import "XNApp.h" @interface XNViewController ()
@property (nonatomic, strong) NSArray *appList;
@property (nonatomic, strong) NSOperationQueue *queue;
@end @implementation XNViewController
#pragma mark - 懒载入 - (NSOperationQueue *)queue {
if (!_queue) _queue = [[NSOperationQueue alloc] init];
return _queue;
} //可抽取出来写到模型中
- (NSArray *)appList {
if (!_appList) {
//1.载入plist到数组中
NSURL *url = [[NSBundle mainBundle] URLForResource:@"apps.plist" withExtension:nil];
NSArray *array = [NSArray arrayWithContentsOfURL:url];
//2.遍历数组
NSMutableArray *arrayM = [NSMutableArray array];
[array enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
[arrayM addObject:[XNApp appWithDict:obj]]; //数组中存放的是字典, 转换为app对象后再加入到数组
}];
_appList = [arrayM copy];
}
return _appList;
} - (void)viewDidLoad {
[super viewDidLoad]; self.tableView.rowHeight = 88; // NSLog(@"appList-%@",_appList);
} #pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.appList.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; //用模型来填充每一个cell
XNApp *app = self.appList[indexPath.row];
cell.textLabel.text = app.name; //设置文字 //设置图像: 模型中图像为nil时用默认图像,并下载图像. 否则用模型中的内存缓存图像.
if (!app.image) {
cell.imageView.image = [UIImage imageNamed:@"user_default"]; [self downloadImg:indexPath];
}
else {
//直接用模型中的内存缓存
cell.imageView.image = app.image;
}
// NSLog(@"cell--%p", cell); return cell;
} /**始终记住, 通过模型来改动显示. 而不要试图直接改动显示*/
- (void)downloadImg:(NSIndexPath *)indexPath {
XNApp *app = self.appList[indexPath.row]; //取得改行相应的模型 [self.queue addOperationWithBlock: ^{
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]]; //得到图像数据
UIImage *image = [UIImage imageWithData:imgData]; //在主线程中更新UI
[[NSOperationQueue mainQueue] addOperationWithBlock: ^{
//通过改动模型, 来改动数据
app.image = image;
//刷新指定表格行
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}];
}];
} @end
上述代码仅仅是做了内存缓存。而每次又一次进入应用时,还会从网上又一次下载。假设要继续优化上面的代码,须要自己去实现本地缓存。
二、使用第三方框架SDWebImage。
(很优秀)
#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.appList.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; //用模型来填充每一个cell
XNApp *app = self.appList[indexPath.row];
cell.textLabel.text = app.name; //设置文字 // //设置图像: 模型中图像为nil时用默认图像,并下载图像. 否则用模型中的内存缓存图像.
// if (!cell.imageView.image) {
// cell.imageView.image = [UIImage imageNamed:@"user_default"];
//
// [self downloadImg:indexPath];
// }
// else {
// //直接用模型中的内存缓存
// cell.imageView.image = app.image;
// } //使用SDWebImage来完毕上面的功能. 针对ImageView.
//一句话, 自己主动实现了异步下载. 图片本地缓存. 网络下载. 自己主动设置占位符.
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:app.icon] placeholderImage:[UIImage imageNamed:@"user_default"]]; return cell;
} /**始终记住, 通过模型来改动显示. 而不要试图直接改动显示*/
//- (void)downloadImg:(NSIndexPath *)indexPath {
// XNApp *app = self.appList[indexPath.row]; //取得改行相应的模型
//
// [self.queue addOperationWithBlock: ^{
// NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]]; //得到图像数据
// UIImage *image = [UIImage imageWithData:imgData];
//
// //在主线程中更新UI
// [[NSOperationQueue mainQueue] addOperationWithBlock: ^{
// //通过改动模型, 来改动数据
// app.image = image;
// //刷新指定表格行
// [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
// }];
// }];
//} @end
SDWebImage中的一些參数:
转载请注明出处:http://blog.csdn.net/xn4545945
【iOS】网络载入图片缓存与SDWebImage的更多相关文章
- IOS 网络浅析-(十三 SDWebImage 实用技巧)
IOS 网络浅析-(十三 SDWebImage 实用技巧) 首先让我描述一下为了什么而产生的实用技巧.(在TableView.CollectionView中)当用户所处环境WiFi网速不够快(不能立即 ...
- iOS网络加载图片缓存策略之ASIDownloadCache缓存优化
iOS网络加载图片缓存策略之ASIDownloadCache缓存优化 在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...
- Android异步载入学习笔记之四:利用缓存优化网络载入图片及ListView载入优化
假设不做不论什么处理.直接用网络载入图片在网速快的情况下可能没什么不好的感觉.可是假设使用移动流量或是网络不好的时候.问题就来了,要么用户会抱怨流量使用太多.要么抱怨图片载入太慢.如论从哪个角度出发, ...
- 玩转iOS开发 - 数据缓存
Why Cache 有时候.对同一个URL请求多次,返回的数据可能都是一样的,比方server上的某张图片.不管下载多少次,返回的数据都是一样的. 上面的情况会造成下面问题 (1)用户流量的浪费 (2 ...
- 【读书笔记】iOS网络-优化请求性能
一,度量网络性能 1,网络带宽 用于描述无线网络性能的最常见度量指标就是带宽.在数字无线通信中,网络带宽可以描述为两个端点之间的通信通道每秒钟可以传输的位数.现代无线网络所能提供的理论带宽是很高的.不 ...
- iOS关于html缓存
方式一:截取请求正则.图片缓存 实现webview缓存网页内容难点在缓存图片上.html代码的缓存相对简单,具体实现思路是这样的:第1步.先获取html页面里所有图片地址.方法一:离线获取获取到htm ...
- ios网络学习------6 json格式数据的请求处理
ios网络学习------6 json格式数据的请求处理 分类: IOS2014-06-30 20:33 471人阅读 评论(3) 收藏 举报 #import "MainViewContro ...
- iOS网络编程模型
iOS网络编程层次结构也分为三层: Cocoa层:NSURL,Bonjour,Game Kit,WebKit Core Foundation层:基于 C 的 CFNetwork 和 CFNetServ ...
- MTNET 自用ios网络库开源
短短两天就在https://git.oschina.net/gangwang/MTNET这里收获15个星 github 5星, 值得收藏! MTNET 自用ios网络库开源, 自用很久了,在数歀上架的 ...
随机推荐
- Objective-C——Runtime理解
动态语言 OC是一门不折不扣的动态语言,所以它的很多机制都是动态运行时决定的.这点和C语言不一样,C语言是静态绑定,也就是编译后所有的一切都已经决定了.这一点和C语言的函数指针有些类似,很多时候函数指 ...
- Python3之切片的道理
list的切片有三个参数:起点,终点,步长 list[::-1] 相当于起点为最后的一个,终点为第一个,然后一次减少一个 更多的看下面的测试 >>> a = [0,1,2,3,4,5 ...
- 结果缓冲区 resbuf具体用法
struct resbuf *rb; rb=acutBulidlist(RTSTR,text(), rtpoint,,,,,) 创建圆用法. acdbEntGet 返回结果缓冲区 然后 rb-> ...
- Android—修改button属性
一般安卓里的普通按钮控件灰灰的,比较单调,我们可以给按钮加上背景图片,或者自定义按钮的圆角,颜色等属性. 下面用代码举例: <Button android:id="@+id/reset ...
- 【LeetCode】2、Add Two Numbers
题目等级:Medium 题目描述: You are given two non-empty linked lists representing two non-negative integers. ...
- Yii2开发技巧 使用类似闭包的方式封装事务
在控制器中执行事务的时候,一般的代码如下: $transaction = Yii::$app->db->beginTransaction(); try { //一些业务代码 $transa ...
- IE7浏览器下去除flash动画边框问题
<object width="100%" height="100%" data="/templates/default/swf/guide.sw ...
- jquery源码分析(五)——Deferred 延迟对象
javascript的异步编程 为什么要使用异步编程? JS是单线程语言,就简单性而言,把每一件事情(包括GUI事件和渲染)都放在一个线程里来处理是一个很好的程序模型,因为这样就无需再考虑线程同步这些 ...
- 【Codeforces 478C】Table Decorations
[链接] 我是链接,点我呀:) [题意] 给你r,g,b三种颜色的气球 每张桌子要放3个气球 但是3个气球的颜色不能全都一样 (允许两个一样,或者全都不一样) 问你最多能装饰多少张桌子 [题解] 先把 ...
- CodeForcesGym 100735B Retrospective Sequence
Retrospective Sequence Time Limit: Unknown ms Memory Limit: 65536KB This problem will be judged on C ...