iOS——调用系统相册和相机
iOS——调用系统相册和相机
背景
我们在许多app里可以发现在换头像的时候一般有两个选择,一个是选择本地照片,一个选择是相机也就是直接拍一张照片当头像,另外还有就是可以去修改照片尺寸,所以我们就要知道这些功能是怎么实现的。
实现方法
首先我们在调用系统相册或者是调用照相机的时候需要用到UIImagePickerController,使用UIImagePickerController 需要有两个代理,分别是:UIImagePickerControllerDelegate,
UINavigationControllerDelegate。
UIImagePickerControllerSourceType //用来设置调用UIImagePickerController的类型
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;//是调用相机。imagePickerController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;// 是调用系统相册。imagePickerController.allowsEditing=YES;//表示可以编辑对照片进行编辑,会多出来一个正方形的框。
首先我们需要在项目的info中添加:
Privacy-Camera Usage Description//App使用相机的权限
Privacy - Photo Library Usage Description//App使用系统相册的权限
我们先在.h文件中添加协议,以及成员变量:
@interface ViewController : UIViewController<UIImagePickerControllerDelegate,
UINavigationControllerDelegate>@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImagePickerController *imagePicker;@end
然后在.m文件中先将iamgeView初始化:
- (UIImageView *)imageView {if (!_imageView) {_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];_imageView.center = self.view.center;_imageView.image = [UIImage imageNamed:@"bgImage.jpg"];_imageView.userInteractionEnabled = YES;//默认为NO,一定要手动设置成YES,这样才可以发生用户交互工作UITapGestureRecognizer *clickTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseImage)];//设置手势识别器,并且添加点击事件[_imageView addGestureRecognizer:clickTap];}return _imageView;
}
然后我们将对应的点击事件函数进行实现:
- (void)chooseImage {self.imagePicker = [[UIImagePickerController alloc] init];self.imagePicker.delegate = self;self.imagePicker.allowsEditing = YES;UIAlertController *action = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;[self presentViewController:self.imagePicker animated:YES completion:nil];}}];UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"从本地相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;[self presentViewController:self.imagePicker animated:YES completion:nil];}];UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {NSLog(@"点击了取消");}];[action addAction:cameraAction];[action addAction:photoAction];[action addAction:cancelAction];[self presentViewController:action animated:YES completion:nil];
}
主要就是添加了一个提示框,根据前边定义的手势识别器去触发函数,达到人机交互功能。
//获取选择的图片
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {[picker dismissViewControllerAnimated:YES completion:nil];UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];self.imageView.image = image;
}//从相机或者相册界面弹出
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {[picker dismissViewControllerAnimated:YES completion:nil];
}
还有另一个方法,可以看看这位大佬:
iOS调用系统相机,访问系统相册功能实现
iOS——调用系统相册和相机相关推荐
- iOS调用系统相册、相机 显示中文标题
2019独角兽企业重金招聘Python工程师标准>>> 最终在info.plist设置解决问题 发现在项目的info.plist里面添加Localized resources can ...
- iOS调用系统相册、相机界面语言设置为中文
在info.plist文件中添加Localized resources can be mixed = YES;
- iOS调用系统相册显示英文标题
调用系统相册.相机,发现是英文的标题"photos",但是手机语言已经设置显示中文,解决办法: 在info.plist里面添加Localized resources can be ...
- iOS 调用系统相册拍照时显示英文问题
在调用系统相册拍照的时候,在选择照片的时候,发现用的都是英文,效果如下: 我们想把那个Retake 和Use Photo 改为对应的汉字,先来一种最笨的方法,最后在来个高级的方法,最笨的办法当然是我们 ...
- 调用系统相册,相机功能,遇到闪退的情况
问题:"This app has crashed because it attempted to access privacy-sensitive data without a usage ...
- IOS 调用系统相册或照相机tab按钮显示中文
1:选中项目,找Supporting Files/xxxx.plist 找Localization native development region value设为en(设为chinese启 ...
- iOS调用系统相册显示英文标题,如何显示中文呢?
在 Info.plist 中 Localized resources can be mixed 设为 YES,意思是允许应用获取框架库内语言. Localization native develop ...
- iOS调用系统相册显示英文
在 Info.plist 中 Localized resources can be mixed 设为 YES,意思是允许应用获取框架库内语言. Localization native developm ...
- ios调用系统相册显示英文,修改成中文
在 Info.plist 中 Localized resources can be mixed 设为 YES,意思是允许应用获取框架库内语言. Localization native develop ...
最新文章
- 什么是产品Backlog(Product Backlog)?
- MediaSource 缓存
- 2020ICPC(上海) - Walker(分类讨论+二分)
- 基于消息与.Net Remoting的分布式处理架构
- 原始socket例子
- 视频通信基础知识之采集
- mysql 索引 数据页_数据库索引数据页
- go的匿名函数和闭包
- html 正则表达式密码判断,JS利用正则表达式实现简单的密码强弱判断实例
- 在无锡调试的工作,到了泰安出错了
- CIM一套完善的消息推送框架
- 高分辨透射电镜(HRTEM)样品怎么制?看这一篇就够了
- excel高级筛选怎么用_关于Excel中“高级筛选”的这些技巧,必须掌握!
- 字体反扒 ---汽车之家(文字)
- 【心电信号】基于matlab心率检测【含Matlab源码 1993期】
- 你关心的问题都在这!爱奇艺用户留存预测挑战赛Baseline上线
- 什么是编程?什么是编程语言?
- Android权限系统(三):运行时权限检查和申请,PermissionController
- 银户通便捷服务加速金融智能化进程
- vue实现斑马线进度条
热门文章
- AOS | 推出无线充电发射器(TX)解决方案
- 回归分析中的p值和R方哪个更重要?
- 量子技术将如何颠覆未来战争形态
- 04 从中兴研发主管坠楼来看,什么是程序员的不能承受之重?
- pcl opencv ROS_message三者之间点云和图片类型转换总结
- jersey java_Jersey 入门与Javabean
- Location定位程序驱动合集
- 郭秀闲:我如何看待埃维诺的未来发展
- RAID 独立冗余磁盘阵列详解(RAID 0、RAID 1、RAID 5、RAID 10)
- 优化策略5 Label Smoothing Regularization_LSR原理分析