高级UIKit-02(文件操作)
【day3_1_Sandbox】:沙箱的介绍
snadbox沙箱沙盒
沙箱根目录下的几个文件夹:
1.应用名称.app存放应用程序的素材
2.Documents:存放应用运行时需要用到的数据(关键性数据),此路径可读可写是经常打交道的一个路径(itunes备份时会备份)
3.Library/Caches:缓存文件夹(itunes备份时不会备份)
4.Library/Preference:用来存放程序的偏好设置,系统提供了api直接操作此文件夹下面的文件
5.tmp:临时文件夹,里面的数据系统会固定隔一段时间清理(itunes备份时不会备份)
沙箱的根目录:
/Users/apple/Library/Application Support/iPhone Simulator/7.0/Applications/A53FEEB4-AC90-4149-A266-92F76F66A53C
1.获取Documents路径方式:
方法一:
NSString *path = NSHomeDirectory();
path = [path stringByAppendingPathComponent:@"Documents"]; // 如果Documents写错了,也不会在根目录创建新的目录
方法二:
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[];
2.获取Caches目录:
NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[];
3.获取临时文件夹目录:
NSString *tmpPath = NSTemporaryDirectory();
4. 写字符串到文件中
NSString *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filePath = [documents stringByAppendingPathComponent:@"b.txt"];
NSError *err = nil;
NSString *str = @"hahah2";
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&err];
if (err) {
NSLog(@"%@",[err localizedDescription]);
}
【dat3_2_Array&Dictionary】:数组和字典的归档、反归档
// 把数组写到plist中
NSArray *namesArray = @[@"张三",@"李四",@"王五",@[@"a",@"b",@"c"]];
// 获取document目录
NSString *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",documents);
// 获取文件路径
NSString *filePath = [documents stringByAppendingPathComponent:@"names.plist"];
// 数组写入到文件中
[namesArray writeToFile:filePath atomically:YES];
// 从plist中加载数组数据
NSString *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",documents);
NSString *filePath = [documents stringByAppendingPathComponent:@"names.plist"];
NSArray *names = [NSArray arrayWithContentsOfFile:filePath];
NSLog(@"%@",names);
// 把字典写到plist中
NSMutableDictionary *personDic = [NSMutableDictionary dictionary];
[personDic setObject:@"小明" forKey:@"name"];
[personDic setObject:[NSNumber numberWithInt:18] forKey:@"age"];
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",path);
NSString *filePath = [path stringByAppendingPathComponent:@"Person.plist"];
[personDic writeToFile:filePath atomically:YES];
// 从plist中加载字典数据
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",path);
NSString *filePath = [path stringByAppendingPathComponent:@"Person.plist"];
NSDictionary *personDic = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSLog(@"%@",personDic);
【Day3_3_FacePlist】:plist文件的使用
scrollView的contentsize属性只要width小于等于320,就不会左右滚动,通常设置为0
手势里面有一个view属性可以获取touch了哪个view
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = @"/Users/tarena/yz/第三阶段(高级UI)/day03/Day3_3_FacePlist/Day3_3_FacePlist/face/emoticons.plist";
// 加载plist
self.faceArray = [NSArray arrayWithContentsOfFile:path];
// 创建scrollview
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, 320, 200)];
int height = self.faceArray.count % 8 == 0 ? self.faceArray.count / 8 * 40 : (self.faceArray.count / 8 + 1) * 40;
// 设置scrollView的contentSize
scrollView.contentSize = CGSizeMake(0, height);
// NSLog(@"%@",faceArray);
// 循环faceArray
for (int i = 0; i < self.faceArray.count; i++) {
NSDictionary *faceDic = [self.faceArray objectAtIndex:i];
// 通过key值取出图片名称
NSString *imageName = [faceDic objectForKey:@"png"];
// NSLog(@"%@",imageName);
UIImageView *faceIV = [[UIImageView alloc] initWithFrame:CGRectMake(i % 8 * 40, i / 8 * 40, 40, 40)];
faceIV.image = [UIImage imageNamed:imageName];
faceIV.tag = i;
faceIV.userInteractionEnabled = YES;// 打开用户交互
// 给faceIV添加手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImage:)];
[faceIV addGestureRecognizer:tap];
[scrollView addSubview:faceIV];
}
[self.view addSubview:scrollView];
}
// 手势方法:点击图片
- (void)clickImage:(UITapGestureRecognizer *)tap{
// 获取点击了哪个图片
UIImageView *iv = (UIImageView *)tap.view;// 使用view属性确定点击了哪个view
// 使用tag属性获取图片在数组中的下标
NSDictionary *faceDic = [self.faceArray objectAtIndex:iv.tag];
NSString *text = [faceDic objectForKey:@"chs"];
self.myTextField.text = [self.myTextField.text stringByAppendingString:text];
}
【Day03_4_NSBundle】
和NSBundle相关的两个方法
1.得到.app文件夹的路径
NSString *appPath = [[NSBundle mainBundle] resourcePath];
2.得到.app文件夹下面的文件路径
NSString *fileAppPath = [[NSBundle mainBundle] pathForResource:@"01" ofType:@"jpg"];
练习:工程中添加一个a.jpg的图片,把它加载到内存中,然后写到documents路径下面
// 得到.app文件夹下面的文件路径
NSString *fileAppPath = [[NSBundle mainBundle] pathForResource:@"a" ofType:@"jpg"];
// 添加一个a.jpg图片到内存中
NSData *fileData = [[NSData alloc] initWithContentsOfFile:fileAppPath];
// 写到documents路径下
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",path);
NSString *filePath = [path stringByAppendingPathComponent:@"a.jpg"];
[fileData writeToFile:filePath atomically:YES];
【Day03_5_FileManager】:文件管理器的使用
- (void)viewDidLoad
{
[super viewDidLoad];
// 创建文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
// 获取范冰冰在.app里面的绝对路径
NSString *directoryPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"范冰冰"];
NSLog(@"%@",directoryPath);
// 使用文件管理器里面的 contentsOfDirectoryAtPath 方法可以获取到一个目录的所有文件
NSArray *imageArray = [fileManager contentsOfDirectoryAtPath:directoryPath error:nil];
// 循环取出数组中的文件名
for (int i = 0; i < imageArray.count; i++) {
NSString *imageName = imageArray[i];
// NSLog(@"%@",imageName);
// 拼接文件名为绝对路径
NSString *imageNamePath = [directoryPath stringByAppendingPathComponent:imageName];
// 创建图片
UIImage *image = [UIImage imageWithContentsOfFile:imageNamePath];
// 创建图片对象
UIImageView *imageIV = [[UIImageView alloc] initWithImage:image];
imageIV.frame = CGRectMake(i % 4 * 80, i / 4 *80, 80, 80);
[self.view addSubview:imageIV];
}
}
【Day03_6_TableViewFileManager】:使用文件管理器获取文件显示在tableView上
MXTableViewController.m主界面
- (void)viewDidLoad
{
[super viewDidLoad];
// 这个路径可以是任何地方,比如说在项目外
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"美女"];
NSLog(@"%@",path);
NSFileManager *fileManager = [NSFileManager defaultManager];
// 从美女目录中获取子目录名称
NSArray *subDirectoryArray = [fileManager contentsOfDirectoryAtPath:path error:nil];
// 循环子目录,把子目录的绝对路径放入数组中,其目的是为了把子目录的绝对路径传给下一个要显示该目录里内容的的界面,【通常传最后一个目录的绝对路径就行】
// self.directroyPathArray = [NSMutableArray array];
for (NSString *directoryName in subDirectoryArray) {
if ([directoryName hasPrefix:@"."]) { // 如果以前缀.开始的文件,干掉
continue;
}
NSString *subDirectoryPath = [path stringByAppendingPathComponent:directoryName];
[self.directroyPathArray addObject:subDirectoryPath];
}
NSLog(@"%@",self.directroyPathArray);
}
cell.textLabel.text = [self.directroyPathArray[indexPath.row] lastPathComponent]; // lastPathComponent这个是字符串的一个方法,其作用是获取路径中最后的那个名称
// 点击cell之后调用此方法,该方法实现了:在跳转页面时发送数据
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *lastDirectoryPath = self.directroyPathArray[indexPath.row];
// 跳转到segue标识为imageList的页面
[self performSegueWithIdentifier:@"imageList" sender:lastDirectoryPath];
}
// 跳转到标识为imageList的页面之前调用该方法,该方法:接收数据并赋值给目的地VC的一个属性
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
MXViewController *imageListVC = segue.destinationViewController;// 目的地页面
imageListVC.directoryPath = sender;
}
MXViewController.m显示图片界面
- (void)viewDidLoad
{
[super viewDidLoad];
// 根据传过来的目录绝对路径,显示图片
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *filesArray = [fm contentsOfDirectoryAtPath:self.directoryPath error:nil];
// 如果目录中有以.为前缀的文件,这么处理
NSMutableArray *lastFilesArray = [NSMutableArray array];
for (NSString *fileName in filesArray) { // 过滤垃圾文件
if (![fileName hasPrefix:@"."]) {
[lastFilesArray addObject:fileName];
// 第二种写法
// NSString *imagePath = [self.direcotryPath stringByAppendingPathComponent:fileName];
// [imagePaths addObject:imagePath];
}
}
for (int i = 0; i < lastFilesArray.count; i++) {
NSString *filePath = [self.directoryPath stringByAppendingPathComponent:filesArray[i]];
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:filePath]];
// 第二种写法
// UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile:imagePath]];
iv.frame = CGRectMake(i % 4 * 80, i / 4 *80, 80, 80);
[self.view addSubview:iv];
}
}
案例总结:
本例使用了动态表视图系统提供的cell原型
使用步骤:
1.拖拽TableViewController
2.新建一个继承自TableViewController的类并和拖拽的绑定
3.选中cell设置identifier属性,使用dequeue起的名字
return 跳出方法
break停止循环
continue结束本次循环进行下一次循环
tableviewcontroller push到一个界面的话,那么tableview里面的每一个cell在点击方法里使用performSegueWithIdentifier都会跳转到这个界面。
如果要在一个界面中显示有多层目录的文件内容,那么就需要传入最后一级目录的绝对路径到该界面。
高级UIKit-02(文件操作)的更多相关文章
- node.js整理 02文件操作-常用API
NodeJS不仅能做网络编程,而且能够操作文件. 拷贝 小文件拷贝 var fs = require('fs'); function copy(src, dst) { fs.writeFileSync ...
- python高级 之(五) --- 文件操作
文件操作 """ 在程序中操作的文件内容: 1. 读取文件中的内容 2. 向文件中写入内容 首先: 在程序中与文件建立一个通道,通过通道操作文件指针,达到所要的结果 向文 ...
- python- shutil 高级文件操作
简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. 拷贝文件 shutil.copyfile(src, ...
- python3之shutil高级文件操作
1.shutil高级文件操作模块 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. 2.shutil模块的拷 ...
- shutil 高级文件操作
High-level file operations 高级的文件操作模块,官网:https://docs.python.org/2/library/shutil.html# os模块提供了对目录或者 ...
- 第3章 文件I/O(7)_高级文件操作:存储映射
8. 高级文件操作:存储映射 (1)概念: 存储映射是一个磁盘文件与存储空间的一个缓存相映射,对缓存数据的读写就相应的完成了文件的读写. (2)mmap和munmap函数 头文件 #include&l ...
- 第3章 文件I/O(6)_高级文件操作:文件锁
7. 高级文件操作:文件锁 (1)文件锁分类 分类依据 类型 说明 按功能分 共享读锁 文件描述符必须读打开 一个进程上了读锁,共它进程也可以上读锁进行读取 独占写锁 文件描述符必须写打开 一个进程上 ...
- python模块之shutil高级文件操作
简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. 注意即便是更高级别的文件复制函数(shutil.co ...
- Linux C高级编程——文件操作之系统调用
Linux C高级编程文件操作之系统调用 宗旨:技术的学习是有限的,分享的精神是无限的. 库函数是一些完毕特定功能的函数.一般由某个标准组织制作公布,并形成一定的标准.使用库函数编 ...
- Python的高级文件操作(shutil模块)
Python的高级文件操作(shutil模块) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 如果让我们用python的文件处理来进行文件拷贝,想必很多小伙伴的思路是:使用打开2个 ...
随机推荐
- Querying Microsoft SQL Server 2012 读书笔记:查询和管理XML数据 2 -使用XQuery 查询XML数据
XQuery 是一个浏览/返回XML实例的标准语言. 它比老的只能简单处理节点的XPath表达式更丰富. 你可以同XPath一样使用.或是遍历所有节点,塑造XML实例的返回等. 作为一个查询语言, 你 ...
- 学习validate
jQuery Validate (转自http://www.w3cschool.cc/jquery/jquery-plugin-validate.html?utm_source=tuicool) jQ ...
- OCP-1Z0-053-V13.02-712新题
Why does the number of blocks for the table remain the sale after the shrink operation? A.Because ...
- struts2 DMI问题
最新开始学习struts2,在官网上下载的最新的struts2(2.3.15.2), jar包,在使用动态方法调用的时候老是报错,错误代码如下HTTP Status 404 - There is no ...
- QT VS检测内存泄漏
测试程序:http://download.csdn.net/detail/ajaxhe/4085447 vld-2.2.3: http://vld.codeplex.com/releases/view ...
- IOS中使用像素位图(CGImageRef)对图片进行处理
IOS中对图片进行重绘处理的方法总结 一.CGImageRef是什么 CGImageRef是定义在QuartzCore框架中的一个结构体指针,用C语言编写.在CGImage.h文件中,我们可以看到下面 ...
- Swift 自定义炫酷下拉刷新效果
先来看下效果 下拉刷新 其实下拉刷新没大家想得那么难.本文已第二个为例子.给大家讲解下下拉刷新的做法(完整代码后面会放上) 首先,先搞一个single View Application .然后进Mai ...
- swift优秀学习博客
http://www.00red.com/ http://www.cnblogs.com/kenshincui/ 优秀的某博客,包含大量iOS的全面的总结 https://github.com/Co ...
- Codeforces Round #246 (Div. 2)
题目链接:Codeforces Round #246 (Div. 2) A:直接找满足的人数,然后整除3就是答案 B:开一个vis数组记录每一个衣服的主场和客场出现次数.然后输出的时候主场数量加上反复 ...
- 【Maven】项目添加Maven类库依赖
1.右击项目-->Maven-->EnableDependencyManagement,按步骤完成操作. 2.右击项目-->Properties-->DeploymentAss ...