摘要:本文将带你了解IOS开发入门iOS开发多图下载程序浅析,希望本文对大家学IOS有所帮助。
本文将带你了解IOS开发入门iOS开发多图下载程序浅析,希望本文对大家学IOS有所帮助。
iOS开发多图下载程序浅析。
效果图如下:
打印效果:
上图打印效果,展现了滚动tableView重复从网络中下载数据的现象,在后面会对上面打印做介绍.
涉及到的知识点:
<code> 01 字典转模型
02 存储数据到沙盒,从沙盒中加载数据
03 占位图片的设置(cell的刷新问题)
04 如何进行内存缓存(使用NSDictionary)
05 在程序开发过程中的一些容错处理
06 如何刷新tableView的指定行(解决数据错乱问题)
07 NSOperation以及线程间通信相关知识</code>
看效果图,感觉很简单,创建一个UITableView,在cell上面设置数据. 以前在都是一些现成的数据,这次试用的数据(图片)是通过URL从网络中下载来的,因此会出现很多问题!
比如:
1. UI很不流畅 --------> 开子线程下载图片
2. 图片重复从网络中下载--------> 把下载过的图片保存起来
3. 图片不会自动刷新
4. 当网络延迟时,图片又会重复下载
5. 数据错乱现象.
首先不考虑上面出现的问题,先把上面的效果图做好.然后再根据上面问题逐一解决.
storyboard
ZYTableViewController文件
这个tableViewController和storyboard中的控制器是绑定好的.
<code>//
// ZYTableViewController.h
// 00-掌握-多图下载综合案例-数据展示
//
// Created by 朝阳 on 2017/11/22.
// Copyright ? 2017年 sunny. All rights reserved.
//
#import <uikit uikit.h="">
@interface ZYTableViewController : UITableViewController
@end
#import "ZYTableViewController.h"
#import "ZYApps.h"
@interface ZYTableViewController ()
@property (nonatomic, strong) NSArray *apps;
@end
@implementation ZYTableViewController
#pragma -mark lazy loading
- (NSArray *)apps
{
if (!_apps) {
// 加载plist文件
NSArray *arrayM = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]];
// 创建一个临时可变数组
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dict in arrayM) {
[tempArray addObject:[ZYApps appWithDict:dict]];
}
_apps = tempArray;
}
return _apps;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.apps.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"app";
// 创建cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
// 设置数据给cell
ZYApps *app = self.apps[indexPath.row];
cell.textLabel.text = app.name;
cell.detailTextLabel.text = app.download;
NSURL *url = [NSURL URLWithString:app.icon];
NSData *iconData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:iconData];
cell.imageView.image = image;
// NSLog(@"%ld----",indexPath.row);
/*
存在两个严重问题:
1. UI很不流畅 ----> 开子线程下载图片
2. 图片重复下载 ----> 先把之前已经下载的图片保存起来
*/
// 返回cell
return cell;
}
@end
</uikit></code>
模型数据ZYApps文件
<code>//
// ZYApps.h
// 00-掌握-多图下载综合案例-数据展示
//
// Created by 朝阳 on 2017/11/22.
// Copyright ? 2017年 sunny. All rights reserved.
//
#import <foundation foundation.h="">
@interface ZYApps : NSObject
/** app名称 */
@property(nonatomic, strong) NSString * name;
/** app下载量 */
@property(nonatomic, strong) NSString * download;
/** app图标 */
@property(nonatomic, strong) NSString * icon;
+ (instancetype)appWithDict:(NSDictionary *)dict;
@end
@implementation ZYApps
+ (instancetype)appWithDict:(NSDictionary *)dict
{
ZYApps *apps = [[ZYApps alloc] init];
// 利用KVC
[apps setValuesForKeysWithDictionary:dict];
return apps;
}
@end
</foundation></code>
以上代码就可以实现效果图.但是存在两个严重的问题:
1. 图片被重复下载
2. UI很不流畅
问题1. 图片被重复下载
因为当滚动tableView的时候,会重复下载网络中的图片.----解决---> 先把下载好的图片保存起来
具体解决:
当应用程序第一次下载下来的时候,tableView中的图片,需要从网络中下载下来.然后把图片保存到内存缓存一份,把图片也写入到沙盒中一份.
当来回滚动tableView的时候,下载过的图片已经在内存中缓存过了,因此获取内存中的图片就可以了.由此防止了重复下载图片的现象.把图片的二进制写入到沙盒中,原因是
因为当应用程序重新启动的时候,在应用程序内存中缓存的图片都清空了,因此还需要重新从网络上下载图片,保存到沙盒中就是为了当重新启动应用程序的时候,数据可以从沙盒中读取,防止重复下载.
此时 tableView:cellForRowAtIndexPath:方法中代码.
<code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"app";
// 创建cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
// 设置数据给cell
ZYApps *app = self.apps[indexPath.row];
cell.textLabel.text = app.name;
cell.detailTextLabel.text = app.download;
// 设置图标
// 查看内存缓存中该图片是否存在,若存在直接用,否则去磁盘缓存中查看是否有缓存\
如果有磁盘缓存,就保存一份到内存.设置图片,否则下载
// 从内存缓存中读取
UIImage *image = [self.images objectForKey:app.icon];
// 是否内存中存在已下载的图片
if (image) {
cell.imageView.image = image;
 
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号