iOS:Documents目录下文件的操作(缓存图片为例)
来源:互联网 发布:sgs报告数据解读 编辑:程序博客网 时间:2024/05/01 05:54
由于允许在Documents目录下比较自由的进行文件操作,因此可以进行诸如网络资源的缓存等功能。
1,添加路径目录
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"MyOwnDirectory"]; NSFileManager *fileManager = [NSFileManager defaultManager]; if([fileManager fileExistsAtPath:path]){ }else{ [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; }
如果该路径不存在则创建该路径
2,删除指定路径的目录或文件
NSString *temPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"MyOwnDiretory"]; NSFileManager *fileManager = [NSFileManager defaultManager]; if([fileManager fileExistsAtPath:temPath]){ [fileManager removeItemAtPath:temPath error:nil]; }
上述可以实现清除缓存目录
3,将图片写到Documents/MyOwnDirectory/下
NSData *image_data = [NSData dataWithData:UIImageJPEGRepresentation(image, 1)]; [image_data writeToFile:filePath atomically:YES];而这个filepath就是指Documents/MyOwnDirectory,可以通过如下方法得到:
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
这个filePath只是Documents/"your file name"如果想要得到/Documents/MyOwnDirectory/"your file name",便需要在documentsDirectory后面添加字符串"MyOwnDirectory/"
0 0
- iOS:Documents目录下文件的操作(缓存图片为例)
- Linux下文件及目录的一些操作(附递归遍历目录源码)
- 关于Documents下文件自动备份到iCloud--(db保存在documents被拒的原因)
- Linux 下获取目录a下文件b的操作
- 关于Android系统目录下文件的操作
- Qt 打开目录下文件图片
- Linux下文件/目录基本操作
- Linux下文件的操作
- Linux下文件的操作
- Linux下文件的操作
- Linux 下文件的操作
- C# 目录操作(新建,删除),获取目录下文件列表
- 统计某个目录下文件的数量
- Linux下文件目录的权限
- 批量编译目录下文件的Makefile
- 查看当前目录下文件的个数
- 目录下文件后缀的更改
- linux下文件与目录的权限
- SSH logins using keys
- Hadoop对于压缩文件的支持及算法优缺点
- //版本更新
- 微信自定义菜单数组组建
- anydvd 破解
- iOS:Documents目录下文件的操作(缓存图片为例)
- Java和JSON之间的转换
- 关于ITUNES
- Binding下
- 指向学生类的指针
- Vmware tool安装
- 关于refs/for/
- Android开发之图片浏览器
- linux ant 安装