1.什么是文件系统?
IOS中每个应用都有自己的文件系统,并有相应的访问权限,一般分为
~/Documents/
~/tmp/
~/Library/Caches/
~/Library/Preferences/-------键值对,不用关心文件路径。
其路径的获取方式为
<span style="color:#999999;">{ //获取主目录 NSString *path=NSHomeDirectory(); NSString *docPath=[path stringByAppendingPathComponent:@"Documents"]; NSLog(@"%@",docPath); //获取文件目录 NSArray *DocumentPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSAllDomainsMask, YES); // NSLog(@"%@",DocumentPath[0]); //获取缓存目录 NSArray *cachePath=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSAllDomainsMask, YES); // NSLog(@"%@",cachePath[0]); //获取临时目录 NSString *temp=NSTemporaryDirectory(); // NSLog(@"%@",temp); }</span>
Plist文件只能存储NSString NSNumber NSData NSArray NSDictionary的内容,其文件存储为xml格式
NSArray存储到Documents中:
NSArray *[email protected][@"name",@"age",@"height"]; NSString *path=NSHomeDirectory(); NSString *docPath=[path stringByAppendingPathComponent:@"Documents"]; NSString *filepath=[docPath stringByAppendingPathComponent:@"/aa.plist"]; //把array存储到plist文件中 [arr writeToFile:filepath atomically:YES]; //从文件路径读取为array NSArray *arr2=[NSArray arrayWithContentsOfFile:filepath];
NSDictionary存储到Cache中:
NSDictionary *[email protected]{@"name":@"lean",@"age":@24,@"height":@172 }; NSArray *dicArr=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSAllDomainsMask, YES); NSLog(@"%@",dicArr[0]); NSString *dirPath=dicArr[0]; NSString *filePath=[dirPath stringByAppendingPathComponent:@"dic.plist"]; //把Dictionary存储到plist文件中 [dic writeToFile:filePath atomically:YES]; //从文件路径读取为Dictionary NSDictionary *dic2=[NSDictionary dictionaryWithContentsOfFile:filePath ];
NSData读取图片:
//读写图片吧能直接存储 只能通过NSData来存储。 //以下例子为从UIImageView中存储文件并在另一个控件中读取显示 NSArray *arr=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSAllDomainsMask, YES); NSLog(@"%@",arr[0]); NSString *cachePath=arr[0]; NSString *filePath=[cachePath stringByAppendingPathComponent:@"image.plist"]; UIImage *image=[self.a image]; NSData *data=UIImageJPEGRepresentation(image,1); [data writeToFile:filePath atomically:YES]; NSData *data2=[NSData dataWithContentsOfFile:filePath]; UIImage *image2=[UIImage imageWithData:data2 ]; self.b.image=image2;
IOS-Plist文件存储(1)
时间: 2024-10-21 19:47:13