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 *arr=@[@"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 *dic=@{@"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;
版权声明:本文博主原创文章,博客,未经同意不得转载。