文件操作——图片保存到本地
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)selectedPhoto:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//创建iamgePickerController对象
UIImagePickerController *ipc = [[UIImagePickerController alloc]init];
//设置sourceType
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
}
}
//获取选中的图片
UIImage *orgImage = [info objectForKey:UIImagePickerControllerOriginalImage];
self.photoImageView.image = orgImage;
//图片保存到本地,
/*
数据持久化,永久保存
1.NSUserDefault 保存少量的数据,几张图片,字符串,小量的数组,字典某个对象
2.文件 图片,视频,一个资源就是一个文件
3.数据库,SQLite3
4.CoreData
应用程序的沙盒
1.获取应用程序允许用户操作的document路径
*/
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
//2.创建文件路径
NSString *filePath = [docPath stringByAppendingPathComponent:@"name"];
NSLog(@"%@",filePath);
//3.判断文件是否存在
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath] == NO) {
//4,创建文件
[[NSFileManager defaultManager]createFileAtPath:filePath contents:nil attributes:nil];
//5.写入数据将UIImage 转化为二进制数据NSData
NSData *imageData = UIImagePNGRepresentation(orgImage);
[imageData writeToFile:filePath atomically:YES];
//读取数据
NSError *errMsg = nil;
NSData *d = [NSData dataWithContentsOfFile:filePath options:NSDataReadingUncached error:&errMsg];
if (errMsg) {
// NSAssert(errMsg, @"cuole");
NSLog(@"%@",errMsg);
return;
}
//二进制NSData转化为UIImage
UIImage *img = [UIImage imageWithData:d];
}
// //将图片报存到系统相册
// UIImageWriteToSavedPhotosAlbum(orgImage, nil, nil,nil);