UIImagePickerController和UIAlertController结合使用
在处理个人资料 - 头像的时候,通常有两个选项,一个是调用系统相机,一个是调用系统相册。这里要使用的就是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];
}
UIImagePickerController和UIAlertController结合使用的更多相关文章
- iOS通过UIAlertController弹出底部选择框来调用相机或者相册
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredSt ...
- iOS8 iPad Warning: Attempt to present <UIImagePickerController:xxxx > on xxxx which is already presenting (null)
解决方法: /* I think this is because in iOS 8, alert views and action sheets are actually presented view ...
- swift2.0 UIImagePickerController 拍照 相册 录像
系统 ios9.1 语言swift2.0 在app 里最常用的功能就是多媒体选择,首先我们storyboard 创建一个button 用于触发选择事件 @IBAction func selectIma ...
- 基于 UIImagePickerController 的拓展封装 - iOS
基于 UIImagePickerController 的拓展,分别支持调用相机.相册的功能,其中相机可以设置默认调用前后摄像头; 简单对此进行了封装和实现,其中还有很多点可以继续挖掘和优化,该版本具体 ...
- iOS8中UIActionSheet弹出UIImagePickerController异常处理
iOS8之后,UIActionSheet改父于UIAlertController.带来了一丢丢兼容性的问题. 比如在弹出的actionsheet中选择从相册选择图片或者拍照,之后弹出UIImagePi ...
- 整理UIImagePickerController问题
[assetsLibrary addAssetsGroupAlbumWithName:@"iOSDevTip1" resultBlock:^(ALAssetsGroup *grou ...
- UIAlertController
楼主在整理项目的警告,于是乎你懂的. 然后自己整理了一下以后方便自己忘了之后能及时找到它 关于UIAlertController .h文件的解析 /** 关于UIAlertController的解析 ...
- iOS UIAlertController跟AlertView用法一样 && otherButtonTitles:(nullable NSString *)otherButtonTitles, ... 写法
今天写弹出框UIAlertController,用alertView习惯了,所以封装了一下,跟alertView用法一样,不说了,直接上代码: 先来了解一下otherButtonTitles:(nul ...
- UIAlertController使用
// 将UIAlertController模态出来 相当于UIAlertView show 的方法// 初始化一个一个UIAlertController // 参数preferredStyle: ...
随机推荐
- C程序中常见的内存操作错误
对C/C++程序员来说,管理和使用虚拟存储器可能是个困难的, 容易出错的任务.与存储器有关的错误属于那些令人惊恐的错误, 因为它们在时间和空间上, 经常是在距错误源一段距离之后才表现出来. 将错误的数 ...
- Strus2学习:基础(一)
Strus2基础: Sturs2起源以及背景: 在起源很早(2002年左右)的 strus1 和 webWork 基础上进行扩展,并且兼容这两大框架!总之很好用啦,随着学习的深入,应该会有更好的诠释的 ...
- struts2值栈内部数据结构详解
值栈是struts2内部一片很重要的区域,我在初学的时候,发现对于值栈这个数据结构的理解不是很深刻.例如OGNLContext是什么,ActionContext和值栈有什么关系.为什么ActionCo ...
- viewPager + fragment
有两种实现方式,一种是 fragmentActivity + FragmentPagerAdapter (Fragment,FragmentManager需要导包:android.support.v4 ...
- latex 竖排子图的生成
latex命令如下: 需要的包为: \usepackage{graphicx} \usepackage{subfigure} \begin{figure*}%加*的作用是跨栏(双栏和单栏latex的区 ...
- Ruby--学习记录(实时更新)
变量的命名方式决定了变量的种类: 局部变量 以英文字母或者_开头: 全局变量 以$开头: 实例变量 以@开头: 类变量 以@@开头:
- 【转】 如何使用Valgrind memcheck工具进行C/C++的内存泄漏检测
系统编程中一个重要的方面就是有效地处理与内存相关的问题.你的工作越接近系统,你就需要面对越多的内存问题.有时这些问题非常琐碎,而更多时候它会演变成一个调试内存问题的恶梦.所以,在实践中会用到很多工具来 ...
- SSIS-包调用包错误的解决方案
1.错误信息: 无法解密受保护的 XML 节点“ DTS:Password”,错误为 0x8009000B“该项不适于在指定状态下使用.”.可能您无权访问此信息.当发生加密错误时会出现此错误.请确保提 ...
- java 的 linq,不要再写令人讨厌的 for 了!
package com.ly.linq; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator ...
- 强大的命令行工具wmic
1.wmic=Microsoft Windows Management Instrumentation 2. C:\WINDOWS\system32\wbem 下的东西,特别是.xsl格式化文件,实现 ...