1埋虹、保存到本地Plist文件
- (IBAction)plistSave:(UIButton *)sender {
//獲取沙盒路徑
NSString *sandBoxPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
//新建路徑存儲plist文件
NSString *plistPath = [sandBoxPath stringByAppendingString:@"/StudentInfo.plist"];
//申明根數(shù)組
NSMutableArray *rootArray = nil;
//判斷沙盒路徑是否存在,根據(jù)沙盒路徑初始化數(shù)組届搁,不存在初始化數(shù)組
if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
//根據(jù)沙盒路徑初始化數(shù)組
rootArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
}else{
rootArray = [NSMutableArray array];
}
//打包數(shù)據(jù)信息
if (self.name.text.length == 0 || self.age.text.length == 0 || self.grade.text.length == 0) {
NSLog(@"信息不完整,存儲失敗");
}else{
NSDictionary *dic = @{@"name":self.name.text,@"age":self.age.text,@"grade":self.grade.text};
[rootArray addObject:dic];
}
//存入plist文件
if ([rootArray writeToFile:plistPath atomically:YES]) {
NSLog(@"存儲成功");
}else{
NSLog(@"存儲失敗");
}
NSLog(@"%@",plistPath);
}
2窍育、從plist文件中讀取數(shù)據(jù)
- (IBAction)plistFetch:(UIButton *)sender {
//獲取沙盒路徑
NSString *sandBoxPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
//新建路徑存儲plist文件
NSString *plistPath = [sandBoxPath stringByAppendingString:@"/StudentInfo.plist"];
//申明根數(shù)組
NSMutableArray *rootArray = nil;
//判斷沙盒路徑是否存在卡睦,根據(jù)沙盒路徑初始化數(shù)組,不存在初始化數(shù)組
if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
//根據(jù)沙盒路徑初始化數(shù)組
rootArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
}else{
rootArray = [NSMutableArray array];
}
//判斷輸入信息是否存在
for (NSDictionary *dic in rootArray) {
if ([dic[@"name"] isEqualToString:self.name.text]) {
NSLog(@"用戶存在");
}
}
}
3漱抓、刪除Plist文件
- (IBAction)plistDeleted:(UIButton *)sender {
//獲取沙盒路徑
NSString *sandBoxPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
//新建路徑存儲plist文件
NSString *plistPath = [sandBoxPath stringByAppendingString:@"/StudentInfo.plist"];
//申明根數(shù)組
NSMutableArray *rootArray = nil;
//判斷沙盒路徑是否存在表锻,根據(jù)沙盒路徑初始化數(shù)組,不存在初始化數(shù)組
if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
//根據(jù)沙盒路徑初始化數(shù)組
rootArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
}else{
rootArray = [NSMutableArray array];
}
}
4乞娄、沙盒文件目錄獲取代碼
//Home
//Home目錄
NSString *homeDirectory = NSHomeDirectory();
//Document
//Document目錄
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
//Cache
//Cache目錄
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
//Libary目錄
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
5瞬逊、圖片存入plist (需要轉(zhuǎn)換)
//獲取沙盒路徑显歧,
NSString *path_sandox = NSHomeDirectory();
//創(chuàng)建一個存儲plist文件的路徑
NSString *newPath = [path_sandox stringByAppendingPathComponent:@/Documents/pic.plist];
NSMutableArray *arr = [[NSMutableArray alloc] init];
UIImage *image = [UIImage imageNamed:@"1.png"];
/*
把圖片轉(zhuǎn)換為Base64的字符串
在iphone上有兩種讀取圖片數(shù)據(jù)的簡單方法: UIImageJPEGRepresentation和UIImagePNGRepresentation.
UIImageJPEGRepresentation函數(shù)需要兩個參數(shù):圖片的引用和壓縮系數(shù).而UIImagePNGRepresentation只需要圖片引用作為參數(shù).通過在實際使用過程中,
比較發(fā)現(xiàn): UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的圖片數(shù)據(jù)量大很多.
譬如,同樣是讀取攝像頭拍攝的同樣景色的照片, UIImagePNGRepresentation()返回的數(shù)據(jù)量大小為199K ,
而 UIImageJPEGRepresentation(UIImage* image, 1.0)返回的數(shù)據(jù)量大小只為140KB,比前者少了50多KB.
如果對圖片的清晰度要求不高,還可以通過設置 UIImageJPEGRepresentation函數(shù)的第二個參數(shù),大幅度降低圖片數(shù)據(jù)量.譬如,剛才拍攝的圖片,
通過調(diào)用UIImageJPEGRepresentation(UIImage* image, 1.0)讀取數(shù)據(jù)時,返回的數(shù)據(jù)大小為140KB,但更改壓縮系數(shù)后,
通過調(diào)用UIImageJPEGRepresentation(UIImage* image, 0.5)讀取數(shù)據(jù)時,返回的數(shù)據(jù)大小只有11KB多,大大壓縮了圖片的數(shù)據(jù)量 ,
而且從視角角度看,圖片的質(zhì)量并沒有明顯的降低.因此,在讀取圖片數(shù)據(jù)內(nèi)容時,建議優(yōu)先使用UIImageJPEGRepresentation,
并可根據(jù)自己的實際使用場景,設置壓縮系數(shù),進一步降低圖片數(shù)據(jù)量大小.
*/
NSData *_data = UIImageJPEGRepresentation(image, 1.0f);
//將圖片的data轉(zhuǎn)化為字符串
NSString *strimage64 = [_data base64EncodedString];
[arr addObject:image64];
//寫入plist文件
if ([arr writeToFile:newPath atomically:YES]) {
NSLog(@"寫入成功");
};
//可以到沙河路徑下查看plist文件中的圖片數(shù)據(jù)
//這樣就存起來的,然后用到的時候再利用存儲的字符串轉(zhuǎn)化為圖片
//NSData *_decodedImageData = [[NSData alloc] initWithBase64Encoding:image64]; 這是iOS7之前的一個方法
NSData *_decodedImageData = [[NSData alloc]initWithBase64EncodedString:strimage64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *_decodedImage = [UIImage imageWithData:_decodedImageData];
//可以打印下圖片是否存在
NSLog(@"===Decoded image size: %@", NSStringFromCGSize(_decodedImage.size));
6确镊、把圖片直接保存到沙盒中士骤,然后再把路徑存儲起來,等到用圖片的時候先獲取圖片的路徑骚腥,再通過路徑拿到圖片敦间。
(1)
//拿到圖片
UIImage *image2 = [UIImage imageNamed:@"1.png"];
NSString *path_document = NSHomeDirectory();
//設置一個圖片的存儲路徑
NSString *imagePath = [path_document stringByAppendingString:@"/Documents/pic.png"];
//把圖片直接保存到指定的路徑(同時應該把圖片的路徑imagePath存起來瓶逃,下次就可以直接用來仁)
[UIImagePNGRepresentation(image2) writeToFile:imagePath atomically:YES];
(2)通過地址取圖片
UIImage *getimage2 = [UIImage imageWithContentsOfFile:imagePath];
NSLog(@"image2 is size %@",NSStringFromCGSize(getimage2.size));
7、歸檔(序列化)
(1)歸檔的類遵守NSCoding協(xié)議厢绝。
(2)實現(xiàn)encodeWithCoder 和initWithCoder方法契沫。
(3)根據(jù)地址進行操作。
好處:可以壓縮存放昔汉。
//歸檔操作
BOOL archiver = [NSKeyedArchiver archiveRootObject:per toFile:self.savePath];
8懈万、CoreData
(1)配置上下文NSManagedObjectContext:在內(nèi)存中開辟一個空間,用于專門處理某件事情靶病,結(jié)束之后去內(nèi)存中讀取上下文会通。鏈接不同功能模塊,保證模塊獨立性娄周。(NSPrivateQueueConcurrencyType私有隊列涕侈,開辟新的線程池,不阻塞主線程煤辨。NSMainQueueConcurrencyType主隊列)裳涛。
NSManagedObjectContext * context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
//配置存儲調(diào)度器
//a.從程序中加載模型
NSManagedObjectModel * model = [NSManagedObjectModel mergedModelFromBundles:nil];
NSPersistentStoreCoordinator * store = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
//b.配置存儲調(diào)度器存儲路徑
NSURL *url = [NSURL fileURLWithPath:self.keyPath];
[store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:nil];
//將調(diào)度器賦值給上下文
context.persistentStoreCoordinator = store;
_context = context;
(2)增刪改查
- (IBAction)action_btn:(UIButton *)sender {
switch (sender.tag) {
case 101:
{
//插入
Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.context];
person.name = self.name.text;
person.phone = self.phone.text;
//保存上下文
[self.context save:nil];
}
break;
case 102:
{
//刪除
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
//在上下文中查找
NSArray *array = [self.context executeFetchRequest:request error:nil];
//匹配數(shù)據(jù)
for (Person *per in array) {
if ([per.name isEqualToString:@"12"]) {
[self.context deleteObject:per];
}
}
//數(shù)據(jù)保存
[self.context save:nil];
}
break;
case 103:
{
//修改
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
//在上下文中查找
NSArray *array = [self.context executeFetchRequest:request error:nil];
//匹配數(shù)據(jù)
for (Person *per in array) {
if ([per.name isEqualToString:@"12"]) {
per.name = @"張三";
}
}
//數(shù)據(jù)保存
[self.context save:nil];
}
break;
case 104:
{
//配置查找條件
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
request.fetchLimit = 10;//查詢長度
request.fetchOffset = 0;//查詢起始點
//查詢條件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name==%@",@"zhangsan"];
request.predicate = predicate;
//在上下文中查找
NSArray *array = [self.context executeFetchRequest:request error:nil];
NSLog(@"%@",array);
}
break;
default:
break;
}
}
過濾條件 ‘*’表示任意字符
beginswith 以...開始
endswith 以...結(jié)束
contaits 包含...
like 像...