主要用的函数和方法上一篇中都有介绍,这里就不在重复了。如果有需要可以自行阅读前文:
这里主要讲解如何使用UIImagePickerController选择照片,显示在UIImageView中并且保存到沙盒中,当下一次在打开应用的时候,可以直接提取。
UIImagePickerController中的常用属性:
①@property(nonatomic) BOOL allowsEditing
是否允许编辑选中的图片
②@property(nonatomic) UIImagePickerControllerSourceType sourceType
设置照片选择器的展示样式
- enum {
- UIImagePickerControllerSourceTypePhotoLibrary ,
- UIImagePickerControllerSourceTypeCamera ,
- UIImagePickerControllerSourceTypeSavedPhotosAlbum
- };
- typedef NSUInteger UIImagePickerControllerSourceType;
viewControllerToPresent
animated:
(
BOOL
)
flag
completion:
(
void (^)(void)
)
completion
展现相关的ViewController。
④- (void)dismissViewControllerAnimated:(BOOL)flag
completion:(void (^)(void))
completion
关闭相关的ViewController。
⑤- (void)imagePickerController:(UIImagePickerController *)
picker
didFinishPickingMediaWithInfo:(NSDictionary *)
info
- #import "ViewController.h"
- @interface ViewController ()
- @property (weak, nonatomic) UIImageView *imageView;
- @end
- @implementation ViewController
- /*
- 需求:
- 从照片库读取一张照片,并且设置界面上的UIImageView
- 再次进入应用时,直接显示上次选择的图像
- 思路:
- 1. 界面上需要一个按钮和一个UIImageView
- 2. 使用UIImagePickerController选择照片并且设置UIImageView的显示
- 3. 保存用户选择的UIImage
- */
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // 设置界面
- UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 300, 300)];
- [imageView setBackgroundColor:[UIColor lightGrayColor]];
- [self.view addSubview:imageView];
- _imageView = imageView;
- UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- [button setFrame:CGRectMake(110, 320, 100, 40)];
- [button addTarget:self action:@selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:button];
- // 1. 判断文件是否存在
- NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *path = [documents[0] stringByAppendingPathComponent:@"image.png"];
- // 注意:此处不要使用imageNamed方法,因为imageNamed方法是从bundle中加载图像的
- UIImage *image = [UIImage imageWithContentsOfFile:path];
- // 2. 如果存在加载图像并且显示在UIImageView中
- if (image != nil) {
- [imageView setImage:image];
- }
- // 从磁盘加载文本文件到字符串
- NSString *strPath = [documents[0] stringByAppendingPathComponent:@"123.txt"];
- // 关于Error参数:
- // 1. 看到autoreleasing描述符,需要实例化一个指针,并且传入指针的地址
- NSError *error = nil;
- NSString *string = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:&error];
- NSLog(@"string %@", string);
- // // 3. 写入NSString的演示
- // NSString *string = @"爱老虎油!!!";
- // // 除非特殊原因,在iOS开发中字符串的编码格式,统一使用UTF8编码
- // [string writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
- }
- #pragma mark UIImagePicker代理方法
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
- {
- NSLog(@"%@", info);
- UIImage *image = info[@"UIImagePickerControllerEditedImage"];
- [_imageView setImage:image];
- // 保存图像时,需要使用NSData进行中转,NSData中间可以存储任意格式的二进制数据
- // 1. 将UIImage转换成NSData
- NSData *imageData = UIImagePNGRepresentation(image);
- // 2. 建立保存文件的路径
- NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *path = [documents[0] stringByAppendingPathComponent:@"image.png"];
- // 3. 将NSData写入文件
- [imageData writeToFile:path atomically:YES];
- // 4. 将NSString写入文件
- NSString *string = @"老虎爱油!!!";
- NSString *strPath = [documents[0] stringByAppendingPathComponent:@"123.txt"];
- [string writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
- // 5. 将info写入Documents保存
- NSString *infoPath = [documents[0] stringByAppendingPathComponent:@"info.plist"];
- [info writeToFile:infoPath atomically:YES];
- NSLog(@"info写入成功吗?");
- [picker dismissViewControllerAnimated:YES completion:nil];
- }
- #pragma mark 按钮监听方法
- - (void)selectPhoto
- {
- NSLog(@"touch me baby");
- // 1. 实例化照片选择器
- UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
- // 2. 设置属性
- [imagePicker setAllowsEditing:YES];
- [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
- [imagePicker setDelegate:self];
- [self presentViewController:imagePicker animated:YES completion:nil];
- }
- @end