您的位置:首页 > 其它

沙盒,plist,NSUser

2016-04-04 00:08 375 查看
沙盒路径,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 = [NSString
stringWithFormat:@"%@/documents/%@",
NSHomeDirectory(), @"ios"];

NSFileManager *fileManager = [NSFileManager
defaultManager];
if([fileManager
fileExistsAtPath:imageDir]){
NSLog(@"存在");
}else{
NSLog(@"不存在");
//创建文件夹路径
[[NSFileManager
defaultManager] createDirectoryAtPath:imageDir
withIntermediateDirectories:YES
attributes:nil
error:nil];
}

UIImage *image = [UIImage
imageNamed:@"1.png"];//设置一个图片的存储路径
NSString *imagePath = [imageDir
stringByAppendingString:@"/aa.png"];//设置一个图片的存储路径
[UIImagePNGRepresentation(image)
writeToFile:imagePath atomically:YES];//写入到ios文件夹中

//保存 .txt 文件到documents下的ios文件夹中
NSString *imageDir = [NSString
stringWithFormat:@"%@/documents/%@",
NSHomeDirectory(), @"ios"];

NSFileManager *fileManager = [NSFileManager
defaultManager];
if([fileManager
fileExistsAtPath:imageDir]){
NSLog(@"存在");
}else{
NSLog(@"不存在");
//创建文件夹路径
[[NSFileManager
defaultManager] createDirectoryAtPath:imageDir
withIntermediateDirectories:YES

attributes:nil
error:nil];
}
NSDictionary *dic =
@{@"content":@"我是测试的",@"name":@"林毛家"};
NSString *filePath = [imageDir
stringByAppendingPathComponent:@"aa.txt"];
NSLog(@"%@",filePath);
if ([dic
writeToFile:filePath atomically:YES]) {
NSLog(@"保存成功");

}else{
NSLog(@"保存失败");
}

//从Documents读取图片图片
NSString *imgPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"/Documents/ios/aa.png"];
UIImage *img = [UIImage
imageWithContentsOfFile:imgPath];

//遍历Documents下的所有文件名称
NSFileManager *fileManager = [NSFileManager
defaultManager];
NSString *DocumentsPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"/Documents"];//获取本地Documents路径
NSError *error =
nil;
NSArray *fileList = [[NSArray
alloc] init];
fileList=[fileManager contentsOfDirectoryAtPath:DocumentsPath
error:&error];
NSLog(@"%@",fileList);

//取出plist文件中的数据
NSString *plistPath = [[NSBundle
mainBundle] pathForResource:@"phone"
ofType:@"plist"];
NSArray *array = [NSArray
arrayWithContentsOfFile:plistPath];
NSMutableArray* dataSource = [NSMutableArray
arrayWithArray:array];
NSLog(@"%@",dataSource);

//NSUserDefaults
的使用
NSUserDefaults *UserDefaults=[NSUserDefaults
standardUserDefaults];
//保存用户头像
UIImage *image=[UIImage
imageNamed:@"aa.png"];
[UserDefaults setObject:UIImagePNGRepresentation(image)
forKey:@"image"];
UIImage *Newimage=[UIImage
imageWithData:[UserDefaults
objectForKey:@"image"]];
//保存用户名
[UserDefaults setObject:@"林毛家"
forKey:@"name"];
NSString *name=[UserDefaults
objectForKey:@"name"];
//判断是否第一次登陆
[UserDefaults setBool:YES
forKey:@"isFirst"];

//从bundel获取文件路径
NSString *theupfilePath=[[NSBundle
mainBundle]pathForResource:@"aa"ofType:@"gif"];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: