沙盒路径,NSBund的使用,plist文件的使用
第一种方法:获取本地documents路径,library路径,temp路径
NSString*documentsPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
NSString*libraryPath=[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES)objectAtIndex:0];
NSString*tempPath=NSTemporaryDirectory();
第二种方法:
NSString*DocumentsPath = [NSHomeDirectory()stringByAppendingPathComponent:@"/Documents"];
NSString*LibraryPath = [NSHomeDirectory()stringByAppendingPathComponent:@"/Library"];
NSString*tmpPath = [NSHomeDirectory()stringByAppendingPathComponent:@"/tmp"];
保存一张图片到documents下的ios文件夹中
NSString*imageDir = [NSStringstringWithFormat:@"%@/documents/%@",NSHomeDirectory(),@"ios"];
NSFileManager*fileManager = [NSFileManagerdefaultManager];
if([fileManagerfileExistsAtPath:imageDir]){NSLog(@"存在");
}else{NSLog(@"不存在");
//创建文件夹路径[[NSFileManagerdefaultManager]createDirectoryAtPath:imageDirwithIntermediateDirectories:YESattributes:nilerror:nil];
}UIImage*image = [UIImageimageNamed:@"1.png"];
//设置一个图片的存储路径NSString*imagePath = [imageDirstringByAppendingString:@"/aa.png"];
//设置一个图片的存储路径[UIImagePNGRepresentation(image)writeToFile:imagePathatomically:YES];
//写入到ios文件夹中
保存 .txt 文件到documents下的ios文件夹中
NSString*imageDir = [NSStringstringWithFormat:@"%@/documents/%@",NSHomeDirectory(),@"ios"];
NSFileManager*fileManager = [NSFileManagerdefaultManager];
if([fileManagerfileExistsAtPath:imageDir]){NSLog(@"存在");
}else{NSLog(@"不存在");
//创建文件夹路径[[NSFileManagerdefaultManager]createDirectoryAtPath:imageDirwithIntermediateDirectories:YESattributes:nilerror:nil];
}
NSDictionary*dic =@{@"content":@"我是测试的",@"name":@"林毛家"};
NSString*filePath = [imageDirstringByAppendingPathComponent:@"aa.txt"];
NSLog(@"%@",filePath);
if([dicwriteToFile:filePathatomically:YES]) {NSLog(@"保存成功");
}else{NSLog(@"保存失败");
}
从Documents读取图片图片
NSString*imgPath = [NSHomeDirectory()stringByAppendingPathComponent:@"/Documents/ios/aa.png"];
UIImage*img = [UIImageimageWithContentsOfFile:imgPath];
遍历Documents下的所有文件名称
NSFileManager*fileManager = [NSFileManagerdefaultManager];
NSString*DocumentsPath = [NSHomeDirectory()stringByAppendingPathComponent:@"/Documents"];
//获取本地Documents路径NSError*error =nil;
NSArray*fileList = [[NSArrayalloc]init];
fileList=[fileManagercontentsOfDirectoryAtPath:DocumentsPatherror:&error];
NSLog(@"%@",fileList);
取出plist文件中的数据
NSString*plistPath = [[NSBundlemainBundle]pathForResource:@"phone"ofType:@"plist"];
NSArray*array = [NSArrayarrayWithContentsOfFile:plistPath];
NSMutableArray*dataSource = [NSMutableArrayarrayWithArray:array];
NSLog(@"%@",dataSource);
NSUserDefaults的使用NSUserDefaults*UserDefaults=[NSUserDefaultsstandardUserDefaults];
//保存用户头像UIImage*image=[UIImageimageNamed:@"aa.png"];
[UserDefaultssetObject:UIImagePNGRepresentation(image)forKey:@"image"];
UIImage*Newimage=[UIImageimageWithData:[UserDefaultsobjectForKey:@"image"]];
//保存用户名[UserDefaultssetObject:@"林毛家"forKey:@"name"];
NSString*name=[UserDefaultsobjectForKey:@"name"];
//判断是否第一次登陆[UserDefaultssetBool:YESforKey:@"isFirst"];
从bundel获取文件路径
NSString*theupfilePath=[[NSBundlemainBundle]pathForResource:@"aa"ofType:@"gif"];
推荐阅读
- Node获取pnpm安装的包源码及代码路径操作
- 音视频+AI,中关村科金助力某银行探索发展新路径 | 案例研究
- 像素风沙盒游戏(我的世界Minecraft)
- QT实现多文件拖拽获取路径的方法
- jupyter|Jupyter修改默认文件保存路径(详细步骤)
- 目录和路径
- java|查看redis安装路径
- 路径管理之隐忧
- Windows批处理(路径中的空格)
- java|Springboot项目访问路径