沙盒路径,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:YES

attributes: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:YES

attributes: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"];

你可能感兴趣的:(沙盒路径,NSBund的使用,plist文件的使用)