记录
- 在写知乎日报的时候用到过imageVIew点击事件,今天刚好借助点击事件学习一下UIImagePickerController
- 今日简单学习了一下如何访问相机和相册,为项目做准备。简单记录
- 在一些应用中,我们需要用到iOS设备的摄像头进行拍照,视频。并且从相册中选取我们需要的图片或者视频。
关于iOS摄像头和相册的应用,可以使用 UIImagePickerController
类来完成控制。
UIImagePickerController
- 引用UIImagePickerController 需用到两个代理
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/2babc8d9448ab57f7bf187f0c342f999.png)
- 以换头像为例,实现调用,由于是虚拟机就不展示相机的调用
- 属性化imageView, 添加点击事件
UIImagePickerController的sourceType
- UIImagePickerControllerSourceTypeCamera;
- 这个是调用相机的时候把
self.imagePickerController.sourceType
设置成相机Type
![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/b416022c623f7a5763d296f827c9905f.png)
- 调用相册时候的设置
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/94e597f450b48255f59512507b3d0702.png)
主要代码
- (void)viewDidLoad {
[super viewDidLoad];
self.imageViewAvatar = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"IMG_2400.JPG"]];
self.imageViewAvatar.userInteractionEnabled = YES;
UITapGestureRecognizer* tapGester = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAvator)];
self.imageViewAvatar.frame = CGRectMake(140, 200, 90, 90);
[self.imageViewAvatar addGestureRecognizer:tapGester];
[self.view addSubview:_imageViewAvatar];
}
点击imageView事件
![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/8346aeddf5be7ace6e73b10937b2fd97.png)
跳转到相机和相册![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/89a355bb623adaf8625379f16cde7b81.png)
imagePickerController:(UIImagePickerController *)picker
是UIImagePickerController提供的三个代理方法之一- 资料如下
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
}
![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/e7862816f85002860b6c071b49980bc7.png)
总结