你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS-Plist文件存儲(1)

IOS-Plist文件存儲(1)

編輯:IOS開發綜合

1.什麼是文件系統?

IOS中每個應用都有自己的文件系統,並有相應的訪問權限,一般分為

~/Documents/

~/tmp/

~/Library/Caches/

~/Library/Preferences/-------鍵值對,不用關心文件路徑。

其路徑的獲取方式為

{
    //獲取主目錄
    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);
}


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;





  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved