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> ...
随机推荐
- 问题-Delphi2007编译时提示内存错误“sxs.dll. No Debug Info.ACCESS 0xXXXXX"
相关资料:http://bbs.csdn.net/topics/340132341 问题现象:在调试程序时,未进工程文件中的TApplication.Run;证明不是代码问题. 问题原因:可能是因为* ...
- poj 1466 Girls and Boys(二分图的最大独立集)
http://poj.org/problem?id=1466 Girls and Boys Time Limit: 5000MS Memory Limit: 10000K Total Submis ...
- hdu 5363 Key Set
http://acm.hdu.edu.cn/showproblem.php?pid=5363 Key Set Time Limit: 2000/1000 MS (Java/Others) Mem ...
- [iOS UI进阶 - 3.2] 手势识别器UIGestureRecognizer
A.系统提供的手势识别器 1.敲击手势 UITapGestureRecognizer numberOfTapsRequired: 敲击次数 numberOfTouchesRequired: 同时敲 ...
- CAAnimation解读
序言 CAAnimation是一个抽象类,遵循了CAMediaTiming协议和CAAction协议!我们不要直接使用CAAnimation类,而是使用其子类: CATransition:提供渐变效果 ...
- linux的shell脚本入门
Linux shell脚本入门教程 为什么要进行shell编程 在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活 的工具.Shell不仅仅是命令的收集,而且是一门非常 ...
- Android系统中Parcelable和Serializable的区别
转载:https://greenrobot.me/devpost/android-parcelable-serializable/ 进行Android开发的时候,我们都知道不能将对象的引用传给Acti ...
- 负载均衡session共享问题
负载均衡+session共享(memcached-session-manager实现) http://www.cnblogs.com/youzhibing/p/5094460.html http:// ...
- SQl函数的写法
USE [ChangHong_612]GO/****** Object: UserDefinedFunction [dbo].[FN_GetProdQty] Script Date: 10/08/20 ...
- Python beautifulsoup模块
BeautifulSoup中文文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ BeautifulSoup下载:http://w ...