Plist 数据库的使用方法


plist
一,简单介绍一下常用的plist文件。
全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件。属性列表文件的扩展名为.plist ,因此通常被称为 plist文件。文件是xml格式的。
Plist文件通常用于储存用户设置,也可以用于存储捆绑的信息 。NSUserDefault 轻量级数据持久化工具 NSUserDefault本质是一个本地的plist文件。只能接收基础数据类型的数据进行存储


二,先说文件保存到的几个位置:
1,工程沙盒里(就是程序user Document文件夹下,以读取文件,写入文件方式)
2,工程自身里(就是在工程里手动创建一个如.plist文件,把固定的内容写入,这个需要人工手动写入)
3,工程沙盒里(保存到user Document下,不过不需要读写文件,用系统的 NSUserDefaults 可以快速保存添加读取删除基本数据类型,类似于android里的Sharedpreferences )

先封装一个 类
//获取 一个文件 在沙盒Library/Caches/ 目录下的路径
+ (NSString *)getFullPathWithFile:(NSString *)urlName {
    
    //先获取 沙盒中的Library/Caches/路径
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *myCacheDirectory = [docPath stringByAppendingPathComponent:@"MyCaches"];
    //检测MyCaches 文件夹是否存在
    if (![[NSFileManager defaultManager] fileExistsAtPath:myCacheDirectory]) {
        //不存在 那么创建
        [[NSFileManager defaultManager] createDirectoryAtPath:myCacheDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    }
    //
    //用md5进行 加密 转化为 一串十六进制数字 (md5加密可以把一个字符串转化为一串唯一的用十六进制表示的串)
    NSString * newName = [urlName MD5Hash];
    
    //拼接路径
    return [myCacheDirectory stringByAppendingPathComponent:newName];
}
//导入头文件
#import <CommonCrypto/CommonDigest.h>
- (NSString *)MD5Hash
{
	const char *cStr = [self UTF8String];
	unsigned char result[16];
	CC_MD5(cStr, strlen(cStr), result);
	return [NSString stringWithFormat:
			@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
			result[0], result[1], result[2], result[3], 
			result[4], result[5], result[6], result[7],
			result[8], result[9], result[10], result[11],
			result[12], result[13], result[14], result[15]];
}

 NSString *path = [self getFullPathWithFile:url];

//读本地
 NSData *data = [NSData dataWithContentsOfFile:path];

 //写本地
 [data writeToFile:path atomically:YES];
 //图片写入本地沙河的方法
  [UIImageJPEGRepresentation(imagV.image, 1) writeToFile:path atomically:YES];

/由于图片存入的是时候使用加密的方式 因此取出的时候要
     //取得一个目录下得所有文件名
        NSArray *filesArr = [[NSFileManager defaultManager] subpathsAtPath: myCacheDirectory];
    NSLog(@"%lu",(unsigned long)filesArr.count);
    for (NSInteger i = 0; i<filesArr.count; i++) {
        NSString *filePath = [myCacheDirectory stringByAppendingPathComponent:filesArr[i]];
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:filePath]];
       // NSData *data = [NSData dataWithContentsOfFile:arr[i]];
    
        //UIImage *imag = [UIImage imageWithData:data];
        [self.dataArr addObject:image];
    
    }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值