【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(文件操作)的更多相关文章

  1. node.js整理 02文件操作-常用API

    NodeJS不仅能做网络编程,而且能够操作文件. 拷贝 小文件拷贝 var fs = require('fs'); function copy(src, dst) { fs.writeFileSync ...

  2. python高级 之(五) --- 文件操作

    文件操作 """ 在程序中操作的文件内容: 1. 读取文件中的内容 2. 向文件中写入内容 首先: 在程序中与文件建立一个通道,通过通道操作文件指针,达到所要的结果 向文 ...

  3. python- shutil 高级文件操作

    简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. 拷贝文件 shutil.copyfile(src, ...

  4. python3之shutil高级文件操作

    1.shutil高级文件操作模块 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. 2.shutil模块的拷 ...

  5. shutil 高级文件操作

    High-level file operations  高级的文件操作模块,官网:https://docs.python.org/2/library/shutil.html# os模块提供了对目录或者 ...

  6. 第3章 文件I/O(7)_高级文件操作:存储映射

    8. 高级文件操作:存储映射 (1)概念: 存储映射是一个磁盘文件与存储空间的一个缓存相映射,对缓存数据的读写就相应的完成了文件的读写. (2)mmap和munmap函数 头文件 #include&l ...

  7. 第3章 文件I/O(6)_高级文件操作:文件锁

    7. 高级文件操作:文件锁 (1)文件锁分类 分类依据 类型 说明 按功能分 共享读锁 文件描述符必须读打开 一个进程上了读锁,共它进程也可以上读锁进行读取 独占写锁 文件描述符必须写打开 一个进程上 ...

  8. python模块之shutil高级文件操作

    简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. 注意即便是更高级别的文件复制函数(shutil.co ...

  9. Linux C高级编程——文件操作之系统调用

    Linux C高级编程文件操作之系统调用 宗旨:技术的学习是有限的,分享的精神是无限的.           库函数是一些完毕特定功能的函数.一般由某个标准组织制作公布,并形成一定的标准.使用库函数编 ...

  10. Python的高级文件操作(shutil模块)

    Python的高级文件操作(shutil模块) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 如果让我们用python的文件处理来进行文件拷贝,想必很多小伙伴的思路是:使用打开2个 ...

随机推荐

  1. Webstorm入门-----常用快捷键

    为了提高敲代码的速度.我们所需要关注的各种快捷键: 首先,快捷键的设置                  相关连接: http://www.cnblogs.com/dc10101/archive/20 ...

  2. iOS 滤镜 转载,原文见正文首行链接

    转载自:http://blog.sina.com.cn/s/blog_5fb39f9101018gv7.html 直接上代码了: // // ViewController.m // 图片模糊处理 // ...

  3. FFTW程序Demo

    #include<stdio.h> #include<stdlib.h> #include <fftw3.h> #include<string.h> # ...

  4. 使用BeautifulSoup解析XML文档

    有200多个XML文档,每个文档类似如下: <?xml version="1.0"?> <VehicleInfo> <FileHeader> & ...

  5. hdoj 2222

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道 AC自动机.....trie树的建立 和 AC自动机的查询,,可作模版... 解题思路:AC的应用 ...

  6. Java疯狂讲义(二)

  7. BZOJ 1614: [Usaco2007 Jan]Telephone Lines架设电话线

    题目 1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farm ...

  8. epoll的LT和ET模式

    原理參考该博客 从man手冊中,得到ET和LT的详细描写叙述例如以下 EPOLL事件有两种模型: Edge Triggered (ET) Level Triggered (LT) 假如有这样一个样例: ...

  9. Eclipse中设置tomcat的启动内存

    现象:眼下每次使用Eclipse启动Tomcat 的时候常常出现OutOfMemoryError thrown from the UncaughtExceptionHandler in thread ...

  10. IllegalArgumentException 介绍

    java.lang 类 IllegalArgumentException java.lang.Object java.lang.Throwable java.lang.Exception java.l ...