在处理个人资料 - 头像的时候,通常有两个选项,一个是调用系统相机,一个是调用系统相册。这里要使用的就是UIImagePickerController方法。

在头像位置的imageView添加一个手势,或者添加一个透明的按钮,用来实现click方法,直接上代码:

- (IBAction)click:(id)sender{//创建提醒视图
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提醒" message:nil preferredStyle:UIAlertControllerStyleActionSheet];UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {//判断设备是否存在摄像头,有就调用系统相机,没有,就提醒用户if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {//创建相机
UIImagePickerController *picker = [[UIImagePickerController alloc] init];//文件由来
picker.sourceType = UIImagePickerControllerSourceTypeCamera; //指定数据来源来自于相机
picker.delegate  = self;// 指定代理
picker.allowsEditing = YES; //允许编辑//模态弹出
[self presentViewController:picker animated:YES completion:nil];}else{//没有摄像头,提醒用户 您的设备没有摄像头
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"您的设备没有摄像头" message:nil preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];[alertController addAction:alertAction1];[self presentViewController:alertController animated:YES completion:nil];}}];[alertController addAction:alertAction];UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {UIImagePickerController *pickerC = [[UIImagePickerController alloc] init];pickerC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//指定数据来源为相册
pickerC.delegate = self;  //指定代理
pickerC.allowsEditing = YES;  // 允许编辑
[self presentViewController:pickerC animated:YES completion:nil];}];[alertController addAction:alertAction2];[self presentViewController:alertController animated:YES completion:nil];}//选取图片之后执行的方法- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{NSLog(@"%@",info);UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];self.photoImage.image = image;[picker dismissViewControllerAnimated:YES completion:nil];}

转载于:https://www.cnblogs.com/Walking-Jin/p/5468379.html

UIImagePickerController和UIAlertController结合使用相关推荐

  1. ios开发学习笔记--调用相册或相机(UIImagePickerController)

    Ios开发相册和相机的使用-UIImagePickerController 在开发中,有时候需要获取用户的相册或者调用相机采集图片,比如APP的头像,此时可以使用UIImagePickerContro ...

  2. ios学习笔记——UIImagePickerController

    1.UIImagePickerController初始化 UIImagePickerController * picker = [[UIImagePickerController alloc]init ...

  3. Swift学习笔记(4)使用UIImagePickerController实现从设备图片库和照相机获取图片

    Swift学习笔记(4)使用UIImagePickerController实现从设备图片库和照相机获取图片 设备图片库和照相机是图像的两个重要来源,使用UIKit中提供的图像选择器UIImagePic ...

  4. Attempt to present UIImagePickerController: 0x1848de00 on UINavigationController: 0x17e6ed50 wh

    Warning: Attempt to present <UIImagePickerController: 0x1848de00>  on <UINavigationControll ...

  5. UIImagePickerController 选照片 拍照 录视频

    最近研究了一下UIImagePickerController,发现以前的写的项目中用UIImagePickerController做的换头像的功能存在很多的不规范之处.so,重新整理一下,不为别的只为 ...

  6. 解决Swift中present(uiImagePickerController,animated: true,completion: nil)闪退的问题

    swift中开发选择图片上传,会使用到Tap Gesture Recognizer控件,对应 UITapGestureRecognizer API,以下是代码示例(取自IOS developer li ...

  7. IOS初级:UIAlertController

    - (IBAction)signOutAction:(id)sender {//初始化,StyleActionSheet是对话框的样式UIAlertController *alert = [UIAle ...

  8. iOS - UIAlertController

    前言 NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController@available(iOS 8.0, *) ...

  9. UIAlertController

    1.alertController 的创建 // 1. 创建时不添加按钮 // 实例化 alertController 对象 UIAlertController *alertController = ...

最新文章

  1. 数据库学习笔记2--MySQL数据类型
  2. android studio 修改程序包名
  3. java编程_Java编程和C语言的比较
  4. Mysql 替换字段的一部分内容
  5. 实现一行或多行文字溢出隐藏显示省略号
  6. [转载] python中set函数是什么数据类型_Python基本数据类型-list-tuple-dict-set详解
  7. MTK C2K mdoem log分析
  8. DIV的摇晃效果---jquery实现
  9. wireshark基本用法及过虑规则
  10. c语言 json解析器,撸一个JSON解析器
  11. CPU测温软件怎么读出来的内核温度?有什么简单靠谱的读取
  12. 深度xp系统插入光盘显示上一张光盘资料解决方法
  13. 《运动改造大脑》总结
  14. PHP 纯汉字包括生僻字的验证方式
  15. python调用通达信数据_通达信数据格式及5分钟信息提取
  16. 2、软件造价总结(主要基准数据)
  17. 【组织架构】中国铁路南宁局集团有限公司
  18. 论坛议程|COSCon'22 云计算(C)
  19. QKL123 | 区块链排行榜
  20. flash保存html,如何把网页上的flash动画保存为swf格式文件(缓存提取)

热门文章

  1. 回文数、罗马数转整数、整数反转
  2. cname 别名记录 解析过程_云解析是什么?需要购买吗?
  3. node install.js 很长_余生很长,放下错的人,才能拥抱属于你的幸福。很唯美的心灵鸡汤...
  4. oracle 日志切换太频繁,关于oracle日志切换的问题
  5. UE4学习-添加机关并添加代码控制
  6. python学习-列表解析、字典解析
  7. python学习-元组tuple(定义、删除、长度、复制、查找、遍历、operator,和列表的区别)
  8. 用c语言实现0到59的数码管,跪求单片机0~99数码管显示用C语言编写的程序
  9. linux cgi命令,Linux之cgi实现系统主机监控
  10. php js 防止重复提交表单,php如何防止form重复提交