iOS笔记--本地文件的保存,读取,删除操作

2017-09-21  本文已影响323人  蚂蚱Damon
在iOS中本地文件以下应该就是常用的

下面记录一些项目中用到的部分

保存

//获取Documents路径
NSArray *Document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * DocumentsPath = [Document objectAtIndex:0];

//获取Caches路径
NSArray *Caches = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [Caches objectAtIndex:0];
/*
自定义的对象要实现NSCoding协议,比如我这是要存入一个地图类型的model
采用plist文件形式来保存
*/
//保存对象到本地
+(void)saveSearchModelToLocal:(AMapTip *)tip
{
/*
具体保存对象时候,我所遇到的问题有以下几点
1,要保存的对象要实现NSCoding归档协议,一般情况第三方比如高德地图中的model一般都已经实现,自定义的要自己实现一下
2,保存对象前要检测下是否已有相同对象,避免重复,
3,保证每次保存的对象都是0的位置,使用insert插入到指定位置
*/
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"demonTest.plist"];

    if (modelArray.count == 0) {
        modelArray = [NSMutableArray arrayWithCapacity:1];
    }
    [modelArray insertObject:tip atIndex:0];
    BOOL saveSuccess = [NSKeyedArchiver archiveRootObject:modelArray toFile:filePath];
    if (saveSuccess) {
        NSLog(@"save status = %d",saveSuccess);
    }
}
/*
项目中的应用场景是,本地视频在显示的时候需要显示缩略图,通过AVURLAsset等部分代码获取之后,将图片保存到本地做一下缓存,下次搜索是否有图片,有就直接加载
*/
//获取路径也是一样的
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
/*
拼接最后完整的路径,这块做的时候遇到个坑,记录如下
拿到上述路径之后,下面部分代码在于将最后文件的路径补全,首先要加上‘/'这个分隔符,然后后面的是文件的名字,最后的效果如下,
/var/mobile/Containers/Data/Application/400BC47D-FBC5-412F-8F55-163E5FBB8264/Documents/thumImage2017_0818_101305_0028_F.jpg
-----之前这个没有加'/’这个分隔符,导致怎么保存之后都拿不到图片
*/
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
NSString *imagePath = [path stringByAppendingString:[NSString stringWithFormat:@"/thumImage%@.jpg",[array firstObject]]];
[UIImagePNGRepresentation(thumb) writeToFile:imagePath atomically:YES];

如果是保存一些简单的信息的话,一般NSUserDefaults用来保存数组,字典什么的就可以了.

读取

//以上面plist保存为例,下面是读取反归档
+ (NSMutableArray *)getSearchModel
{
//一般情况下,即便是没保存的时候应该需要先去读取的,所以,最好判断是文件的存在与否
    NSString *filePath = [self getDocumentPath];
    NSFileManager *fileM = [NSFileManager defaultManager];
    if (![fileM fileExistsAtPath:filePath]) {
        [fileM createFileAtPath:filePath contents:nil attributes:nil];
    }
    NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    return array;
}
- (void)getImage:(NSString *)fileName
{
/*
读取图片其实就是保存的逆过程
1,在下面这个创建UIImage的方法中,之前都是传入文件的名字,而且我都是在工程里已经有该图片的时候才会使用这个方法,但是这次误打误撞之下传入了我拿到的图片文件的路径,没想到也可以显示。
2,本来该方法是配合获取视频的缩略图然后要做缓存,imageNamed该方法系统会做缓存,那么图片只会加载一次,目前来看配合TableView还行,没有尝试大量数据
    UIImage *img = [UIImage imageNamed:imagePath];
*/
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSArray *array = [fileName componentsSeparatedByString:@"."];
    NSString *imagePath = [path stringByAppendingString:[NSString stringWithFormat:@"/thumImage%@.jpg",[array firstObject]]];
    UIImage *img = [UIImage imageNamed:imagePath];
}

删除

//以上面保存的图片为例
- (void)deleteFileWith:(NSString *)fileName
{
/*
在通过文件的名字获取到文件路径之后,通过NSFileManage来删除某个路径的文件
*/
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *docuPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *localFilePath = [NSString stringWithFormat:@"%@/%@",[docuPaths lastObject],fileName];
    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:localFilePath];
    if (blHave) {
        BOOL blDele= [fileManager removeItemAtPath:localFilePath error:nil];
        if (blDele) {
            NSLog(@"dele success");
        }else {
            NSLog(@"dele fail");
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读