• UI高级-沙盒


    【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)[0];

    2.获取Caches目录:

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

    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都会跳转到这个界面。

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

  • 相关阅读:
    User类 新增共有属性Current ID
    关于multi-label classification中评价指标的一些理解
    Improved Few-Shot Visual Classification草草阅读
    文献阅读:A New Meta-Baseline for Few-Shot Learning
    Windows 10系统在Anaconda下安装GPU版Pytorch
    第九章实验
    实验 5 编写、调试具有多个段的
    实验 4 [bx]和loop的使用
    实验三
    实验 2 用机器指令和汇编指令编程
  • 原文地址:https://www.cnblogs.com/deng37s/p/4572439.html
Copyright © 2020-2023  润新知