zoukankan      html  css  js  c++  java
  • iOS xcode里文件夹下的文件获取方法

    注意:
    NSBundle is used to access resources within your application itself: that is, everything inside YourApp.app. The documentsDirectory is a location outside of your app -- it's in the "home directory" which is part of your app sandbox, and which is analogous to your user home directory on the Mac. 
     
    1.这个是获取[[NSBundle mainBundle] bundlePath]]的文件
     
    NSDirectoryEnumerator *myDirectoryEnumerator;
        NSFileManager *myFileManager=[NSFileManager defaultManager];
        
        myDirectoryEnumerator=[myFileManager enumeratorAtPath:[[NSBundle mainBundle] bundlePath]];
        
        NSString *docPath;
        docPath = [self documentsPath]; //得到文件的路径
        NSLog(@"%@", docPath);
        _filePathArray = [[NSMutableArray alloc]init]; //用来存目录名字的数组
        localFileManager=[[NSFileManager alloc] init]; //用于获取文件列表
        
        NSString *file;
        
        while((file=[myDirectoryEnumerator nextObject])) //遍历当前目录
        {
    // NSLog(@"%@",[file lastPathComponent]);
            
            if ([[file lastPathComponent] isEqualToString:@"cover.jpg"]) {
                [_filePathArray addObject:[docPath stringByAppendingPathComponent:file]]; //存到数组
    // NSLog(@"%@",file);
                NSLog(@"%@",_filePathArray);
            }
            
    // if([[file pathExtension] isEqualToString:@"json"]) //取得后缀名这.png的文件名
    // {
    //           
    // }
            
        }
     
     
    2.这个是获取沙盒里面的文件
     
     NSDirectoryEnumerator *myDirectoryEnumerator;
        NSFileManager *myFileManager=[NSFileManager defaultManager];
        
        myDirectoryEnumerator=[myFileManager enumeratorAtPath:NSHomeDirectory()];
        
        NSString *docPath;
        docPath = [self documentsPath]; //得到文件的路径
        NSLog(@"%@", docPath);
        _filePathArray = [[NSMutableArray alloc] init]; //用来存目录名字的数组
        _JSONPathArray = [NSMutableArray array];
        localFileManager=[[NSFileManager alloc] init]; //用于获取文件列表
        
        NSString *file;
        
        while((file=[myDirectoryEnumerator nextObject])) //遍历当前目录
        {
            // NSLog(@"%@",[file lastPathComponent]);
            
            if ([[file lastPathComponent] isEqualToString:@"cover.jpg"]) {
                [_filePathArray addObject:[docPath stringByAppendingPathComponent:file]]; //存到数组
                // NSLog(@"%@",file);
                NSLog(@"%@",_filePathArray);
            }
            
            if([[file lastPathComponent] isEqualToString:@"target.json"]) //取得后缀名这.json的文件名
            {
                [_JSONPathArray addObject:[docPath stringByAppendingPathComponent:file]];
                NSLog(@"%@",_JSONPathArray);
            }
            
        }
     
     
            NSString *imagePath = _filePathArray[indexPath.item];
    获取在真机bundle里面的文件夹里的图片,不能用全路径
            NSArray *arr = [imagePath componentsSeparatedByString:@"/Documents/"];
            NSString *strSubStringDigNum = [arr objectAtIndex:1];
            UIImage *imageLocal = [UIImage imageNamed:strSubStringDigNum];
    下面是获取在真机沙盒里面的文件夹里的图片,要用全路径,而且不能用 imageNamed:imagePath
    如果用imageNamed:imagePath 会出现只能获取第一个文件夹的图片
            UIImage *imageDownload = [UIImage imageWithContentsOfFile:imagePath];
            cell.image = imageLocal ? imageLocal : imageDownload;
  • 相关阅读:
    【持续更新】Android 源码下载地点
    android Notification 的使用
    如何设置 Notification 中PendingIntent 的 Intent
    CodeSmith部分类型转换代码
    【转】Request.ServerVariables参考
    ADT
    根据IP从纯真IP数据库查询地址
    VS2008重置默认环境
    Eclipse 安装SVN插件
    C#序列化JSON对象
  • 原文地址:https://www.cnblogs.com/hl-iOS/p/5856532.html
Copyright © 2011-2022 走看看