iOS通过UIAlertController弹出底部选择框来调用相机或者相册
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) { }]; UIAlertAction *camera = [UIAlertAction actionWithTitle:@"打开相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentViewController:imagePicker animated:YEScompletion:nil]; }]; UIAlertAction *picture = [UIAlertAction actionWithTitle:@"打开相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType]; }
pickerImage.delegate = self;
pickerImage.allowsEditing = NO; [self presentViewController:pickerImage animated:YEScompletion:nil];
}];
[alertVc addAction:cancle];
[alertVc addAction:camera];
[alertVc addAction:picture];
[self presentViewController:alertVc animated:YES completion:nil]; // 上面写到Button的点击事件方法里,下面直接复制就OK,记得遵守代理协议
UIImagePickerControllerDelegate, UINavigationControllerDelegate // 选择图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{ NSString *type = [info objectForKey:UIImagePickerControllerMediaType]; //当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
//先把图片转成NSData
UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data;
if (UIImagePNGRepresentation(image) ==nil)
{
data = UIImageJPEGRepresentation(image,1.0);
}
else
{
data = UIImagePNGRepresentation(image);
} //图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString *DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; //文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager]; //把刚刚图片转换的data对象拷贝至沙盒中并保存为image.png
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/userHeader.png"] contents:data attributes:nil]; //关闭相册界面
[picker dismissViewControllerAnimated:YEScompletion:nil]; //加在视图中
[self.mineView.headerButton setBackgroundImage:image forState:(UIControlStateNormal)]; }
}
// 取消选取图片
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YEScompletion:nil];
}
也可以按照需求对UIAlertAction进行个性化设置,比如说改变字体颜色,字体大小等等
UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
}];
//修改按钮文字颜色
[cancle setValue:[UIColor yellowColor] forKey:@"titleTextColor"];
更多个性化设置可以参考下面资料
iOS通过UIAlertController弹出底部选择框来调用相机或者相册的更多相关文章
- pushbutton 移动端弹出列表选择框
pushbutton 移动端弹出列表选择框 移动端从下往上推动画效果端弹出列表选择框,适应所有主流移动端机型,支持Node引入,require引入;如有用得不爽可以随时提意见,谢谢. demo地址: ...
- 【iOS】UIAlertController 弹出框
UIAlertView 虽然还能用,但已经废弃了.因此以后尽量用 UIAlertController.示例代码如下: UIAlertController *alert = [UIAlertContro ...
- android 弹出日期选择框
DatePickerDialog 在很多时候需要用户去设定时间,不可能让用户去在一个文本框中去输入时间,所以就需要有个日期弹出选择框,而这个框就是DatePickerDialog. 1.在API中的D ...
- layui文件上传中如何先判断后再弹出文件选择框
前言:layui中的上传,如何在点击上传按钮前阻止file的默认打开文件选择框呢?我想点击后先判断下,如果判断结果不符合,就直接弹出提示框,而不是文件选择框,判断符合才进行文件选择.但是在layui的 ...
- MUI 底部弹出的选择框
大致是这样: 第一种方法:这种方法是使用5+的nativeUI原生动画 1)引入:mui.css或者mui.min.css mui.js或者mui.min.js也行 mui.picker.min.js ...
- js点击某个图标或按钮弹出文件选择框
<HTML> <head> <script type="text/javascript" src="script/jquery-1.6.2. ...
- 清除ios系统alert弹出框的域名
清除ios系统alert弹出框的域名 <script> window.alert = function(name) { var iframe = document.createElemen ...
- iOS开发——UI篇&下拉弹出列表选择项效果
下拉弹出列表选择项效果 右边菜单中的按键,点击弹出一个列表可选择,选择其中一个,响应相应的事件并把文字显示在右边的菜单上:弹出下拉效果使用LMDropdownView插件,可以用POD进行加载pod ...
- 弹出JS提示框
弹出JS提示框Page.ClientScript.RegisterStartupScript(typeof(string), "msg", "<script> ...
随机推荐
- String比较
String str1 = "abc"; String str2 = "abc"; String str3 = new String("abc&quo ...
- Web Service学习之一:Web Service原理
一.定义 Web Service 不是框架也不是技术 而是解决远程调用.跨平台调用.跨语言调用问题的一种规范. 二.应用1.同一个公司新.旧系统的整合:比如CRM系统与OA.客服系统相互调用2.不同公 ...
- 【转】在企业内部分发 iOS 应用程序
(via:破船之家,原文:Provision iOS IPA App for In-House Enterprise Distribution) 在企业内部分发 iOS 应用程序非常复杂.经过努力 ...
- oracle中DECODE与CASE的用法区别
对于CASE与DECODE其实并没有太多的区别,他们都是用来实现逻辑判断.Oracle的DECODE函数功能很强,灵活运用的话可以避免多次扫描,从而提高查询的性能.而CASE是9i以后提供的语法,这个 ...
- Gym 100507L Donald is a postman (水题)
Donald is a postman 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/L Description Donald ...
- POJ 1797 Heavy Transportation (dijkstra 最小边最大)
Heavy Transportation 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Backgro ...
- 斯特灵数 (Stirling数)
@维基百科 在组合数学,Stirling数可指两类数,都是由18世纪数学家James Stirling提出的. 第一类 s(4,2)=11 第一类Stirling数是有正负的,其绝对值是个元素的项目分 ...
- mysql的interval函数用法
Name: 'INTERVAL' Description: Syntax: INTERVAL(N,N1,N2,N3,...) Returns 0 if N < N1, 1 if N < N ...
- 插入三层treeview代码
#region treetView加载 private void treeViewLoad() { DataView dv = navds.tbSiteKind.AsDataView(); treeV ...
- DX相机变换矩阵推导
网上很多的推导过程都是错的,所以写一个. 先平移,再旋转就可以,先平移的原因是,如果先旋转的话,坐标系已经改了,所以先平移. 平移的变换和相机的变换是相反的,所以是: 平移完成后,相机的位置就和原点的 ...