高级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个 ...
随机推荐
- swiper,animate使用方法
1.先链接css和js文件 <link rel="stylesheet" type="text/css" href="css/swiper-3. ...
- Aspx 页面生命周期
ASP.NET 页运行时,此页将经历一个生命周期,在生命周期中将执行一系列处理步骤.这些步骤包括初始化.实例化控件.还原和维护状态.运行事件处理程序代码以及进行 呈现.了解页的生命周期非常重要,这样就 ...
- 利用jQuery获取数据,JSONP
最近工作用到了跨域请求,所以此文就有了,概念网上都有,就不细说了,直接来了. 看了一篇文章,说的是通过扩展让ASP.NET Web API支持JSONP,jsonp网上有很多的教程,js代码部分基本和 ...
- java之路径
在学习java的过程中经常遇见关于路径的问题:到底是使用绝对路径还是相对路径? 个人推荐使用相对路径,如果是使用绝对路径,到后期项目发布的时候会出现许多问题. 那么相对路径怎么用来: 一. Syste ...
- Android 学习开发笔记《Android认识》
1. Android:2007年11月5日第一版,2009年5月豪华版,2010年HTC手机 2. Android框架主要:应用程序.应用程序框架.函数库.运行时. ...
- Python每日一练(3):爬取百度贴吧图片
import requests,re #先把要访问URL和头部准备好 url = 'http://tieba.baidu.com/p/2166231880' head = { 'Accept': '* ...
- U-Boot在FL2440上移植(四)----支持网卡DM9000和烧写yaffs文件系统
<一>支持网卡芯片DM9000 在driver下,有网卡驱动DM9000x.c 和 DM9000x.h DM9000接在BANK4,位宽16 在include/configs/TX2440 ...
- AutoCAD 2014简体中文版官方正式版x86 x64下载,带注册机,永久免费使用
注册机使用说明:会有部分杀毒软件报病毒,请无视.操作步骤:1.安装Autodesk AutoCAD 20142.使用以下序列号666-69696969安装.3.产品密码为001F14.安装完成后,启动 ...
- hibernate 事务的隔离级别 5.1
脏读不可重复读幻读可序列化(符合事务的四个特性的正常情况 ) 解释: 脏读:事务A对数据1做了更新,但是还没有来得及提交 此时事务B对数据1进行了查询获得了事务A更新后的数据, 但是事务A因为一些原因 ...
- Xamarin.Android开发实践(五)
原文:Xamarin.Android开发实践(五) 一.服务的生命周期 服务与活动一样,在它的整个生命周期中存在着一些事件,下图可以很好解释整个过程以及涉及到的方法: 在真实的使用中,Service来 ...