首页 > 其他 > 详细

高级UIKit-02(文件操作)

时间:2014-02-11 00:33:26      阅读:348      评论:0      收藏:0      [点我收藏+]

【day3_1_Sandbox】

snadbox:沙箱、沙盒

沙箱根目录下的几个文件夹:

1.应用名称.app:存放应用程序的素材

2.Documents:存放应用运行时需要用到的数据(关键性数据),此路径可读可写是经常打交道的一个路径(itunes备份时会备份)

3.Library/Caches:缓存文件夹(itunes备份时不会备份)

4.Library/Preference:用来存放程序的偏好设置,系统提供了api直接操作此文件夹下面的文件

5.tmp:临时文件夹,里面的数据系统会固定隔一段时间清理(itunes备份时不会备份)

mac中沙箱的根目录:

/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)[0]; // 推荐使用这种方式,以免Documents写错

2.获取Caches目录:

NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];

3.获取临时文件夹目录:

NSString *tmpPath = NSTemporaryDirectory();

 

4. 写字符串到文件中

bubuko.com,布布扣
    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]);

    }
bubuko.com,布布扣

【dat3_2_Array&Dictionary】

从plist中加载数组数据

arrayWithContentsOfFile:filePath

从plist中加载字典数据

dictionaryWithContentsOfFile:filePath

【Day3_3_FacePlist】

scrollView的contentsize属性只要width小于等于320,就不会左右滚动,通常设置为0

手势里面有一个view属性可以获取touch了哪个view

使用手势的view属性可以获取到点击了哪个view

bubuko.com,布布扣
// 手势方法:点击图片
- (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];
}
bubuko.com,布布扣

【Day03_4_NSBundle】

和NSBundle相关的两个方法

1.得到.app文件夹的路径

NSString *appPath = [[NSBundle mainBundle] resourcePath];

2.得到.app文件夹下面的文件路径

NSString *fileAppPath = [[NSBundle mainBundle] pathForResource:@"01" ofType:@"jpg"];

练习:工程中添加一个a.jpg的图片,把它加载到内存中,然后写到documents路径下面

bubuko.com,布布扣
- (void)viewDidLoad
{
    [super viewDidLoad];
    // 得到.app文件夹的路径
    NSString *appPath = [[NSBundle mainBundle] resourcePath];
    // 得到.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];
    
}
bubuko.com,布布扣

【Day03_5_FileManager】

文件管理器的使用,显示文件夹中的图片到界面上

bubuko.com,布布扣
- (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];
    }
    
}
bubuko.com,布布扣

【Day03_6_TableViewFileManager】案例总结:

return 跳出方法

break停止循环

continue结束本次循环进行下一次循环

 

tableviewcontroller  push到一个界面的话,那么tableview里面的每一个cell在点击方法里使用performSegueWithIdentifier都会跳转到这个界面。

 

如果要在一个界面中显示有多层目录的文件内容,那么就需要传入最后一级目录的绝对路径到该界面。

 

高级UIKit-02(文件操作)

原文:http://www.cnblogs.com/yangmx/p/3543474.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!