将文件保存在Documents/fileName下

本文详细介绍了如何在iOS沙盒中获取指定文件夹下的图片路径,包括创建文件夹、获取Documents路径、拼接文件名等关键步骤,并通过实例展示了获取沙盒下图片路径的具体方法。
<span style="font-family: Arial, Helvetica, sans-serif;">+ (NSString*)GetPath:(NSString*)imageName inFileName:(NSString*)fileName</span>
{
    //imageName : 要保存的图片/文件的名字
    //fileName  : Documents下创建的文件夹的名字
    
    NSString *paths = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:fileName];
    
    //创建文件夹
    BOOL creatManager = [[NSFileManager defaultManager] createDirectoryAtPath:paths withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *fullPathToFile = [paths stringByAppendingPathComponent:imageName];
    NSLog(@"~~~~~~~~~~~~~~~~~~~~ fullPathToFile ~~~~~~~~~~~~~~~~~~  %@",fullPathToFile);

    return fullPathToFile;
 }


获取沙盒Documents路径

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];



获取沙盒下图片的路径 1


 NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    NSString * filePath = [documentsDirectory stringByAppendingPathComponent:imageName];


获取沙盒下图片的路径 2
NSString *paths = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];


创建沙盒目录下文件夹,返回BOOL类型的值。 yes表示创建成功

[[NSFileManager defaultManager] createDirectoryAtPath:paths withIntermediateDirectories:YES attributes:nil error:nil]


获取沙盒目录文件夹下的图片路径

//paths为前面创建的文件夹路径(必须先创建文件夹再获取下面的路径)
NSString *fullPathToFile = [paths stringByAppendingPathComponent:imageName];




第一部分:目录与文件操作1.创建目录结构在当前用户主目录下创建以下目录结构:~/exam/ ├── documents/ │ ├── personal/ │ └── work/ ├── projects/ │ ├── python/ │ └── shell/└── backups/在 ~/exam/documents/personal/ 下创建 notes/ 子目录2.文件创建与内容编辑在 ~/exam/projects/shell/ 目录下创建三个文件:script1.sh 内容为 #!/bin/bash 和 echo "First script"script2.sh 内容为 #!/bin/bash 和 echo "Second script"data.txt 内容为5行随机单词在 ~/exam/documents/work/ 下创建 report.txt,包含3行任意文本内容第二部分:文件操作与管理 3.文件复制与移动将 ~/exam/projects/shell/ 下的所有 .sh 文件复制到 ~/exam/backups/将 ~/exam/documents/work/report.txt 移动到 ~/exam/documents/personal/notes/ 并重命名为 work_report.txt在 ~/exam/ 目录下创建 projects/python/ 的符号链接,名为 python_projects4. 文件删除与恢复删除 ~/exam/projects/shell/data.txt 文件清空 ~/exam/backups/ 目录下的所有内容第三部分:权限与属性 5.文件权限设置设置 ~/exam/projects/shell/script1.sh 的权限为:所有者:读、写、执行组用户:读、执行其他用户:无权限修改 ~/exam/documents/ 目录权限为:所有者:所有权限组用户:读和执行其他用户:读将 ~/exam/projects/shell/script2.sh 的所有者改为 root(需要sudo)6.文件查找与统计查找 ~/exam/ 目录下所有最近7天内修改过的文件统计 ~/exam/documents/ 目录及其子目录下的文件总数查找系统中所有大于10MB且属于当前用户的文件第四部分:综合应用7.归档与压缩将整个 ~/exam/documents/ 目录打包为 documents.tar将 ~/exam/projects/ 目录压缩为 projects.tar.gz创建一个仅包含 ~/exam/backups/ 内容的zip压缩包 backups.zip8.文本处理在 ~/exam/ 下创建 users.txt 文件,内容为:alice:1001bob:1002charlie:1003david:1004alice:1005提取所有用户名并排序去重统计每个名字出现的次数将所有用户ID大于1003的行保存到 new_users.txt
04-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值