iOS笔记之文件读写
2015-06-08 16:58
393 查看
最近做的项目中要存储一组图片数据,图片带有name,date等属性,处理办法是讲image图片直接存在沙盒documents文件中,再使用plist文件存储图片属性和image路径。
存入图片:
读取图片:
存入图片:
- (void)savePhotos:(NSArray *)photos { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *plistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"photo_info.plist"]; NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image"]; NSLog(@"%@", filePath); NSFileManager* fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:plistPath]) { [fileManager createFileAtPath:plistPath contents:nil attributes:nil]; } if (![fileManager fileExistsAtPath:filePath]) { [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:NO attributes:nil error:nil]; } NSMutableArray *savePhotos = [[NSMutableArray alloc] init]; for (Photo *photo in photos) { NSMutableDictionary *info = [[NSMutableDictionary alloc]init]; NSString *imagePath = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", photo.name]]; [UIImagePNGRepresentation(photo.image)writeToFile:imagePath atomically:YES]; [info setObject:imagePath forKey:@"ImagePath"]; [info setObject:photo.name forKey:@"Name"]; [info setObject:photo.tags forKey:@"Tags"]; [info setObject:photo.date forKey:@"Date"]; [savePhotos addObject:info]; } [savePhotos writeToFile:plistPath atomically:YES]; }
读取图片:
- (NSArray *)loadPhotosFromDisk { //从本地读取图片 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *plistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"photo_info.plist"]; NSArray *photosArr = [NSArray arrayWithContentsOfFile:plistPath]; for (NSDictionary *dic in photosArr) { Photo *photo = [[Photo alloc] init]; photo.image = [UIImage imageWithContentsOfFile:[dic valueForKey:@"ImagePath"]]; photo.name = [dic valueForKey:@"Name"]; photo.tags = [dic valueForKey:@"Tags"]; photo.date = [dic valueForKey:@"Date"]; [self.photos addObject:photo]; } return self.photos; }
相关文章推荐
- 小胖说事17--------iOS应用内支付(IAP)的那些坑建测试账号流程!
- 小胖说事16--------详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!
- 如何更改iOS项目名称
- 小胖说事15--------iOS 应用内付费(IAP)开发步骤(最全)
- 转载,自用,iOS中成员变量和属性的区别
- iOS 根据cell内容设置cell的高度
- iOS 关于音乐图片下载到本地沙盒的一些事
- IOS7设计指南
- Arteriosclerosis, Arteriolosclerosis, Atherosclerosis的区别
- [IOS]学习
- IOS笔记044-通知和代理(观察者模式和代理模式)
- Ios开发随笔之block(一)
- github优秀开源项目大全-iOS
- iOS 数据持久化
- Github上的iOS开源类库
- iOS中判断照片和相机权限
- iOS中判断照片和相机权限
- IOS菜鸟的所感所思(十)——专辑接口和专辑中歌曲数据的获得(上)
- 学习iOS开发前应该知道的——第一部分
- iOS 过滤emoji表情