1.首先要遵守三个协议

UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate

2.主要代码

//选择头像
- (IBAction)handleSelectImage:(id)sender {self.myActionSheet = [[UIActionSheet alloc]initWithTitle:nildelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"打开照相机",@"从相册中获取", nil];[self.myActionSheet showInView:self.view];
}
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{if (buttonIndex == self.myActionSheet.cancelButtonIndex) {NSLog(@"你取消了");}switch (buttonIndex) {case 0:[self takePhoto];break;case 1:[self localPhoto];break;default:break;}
}
//照相
- (void)takePhoto{UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){UIImagePickerController *picker = [[UIImagePickerController alloc] init];picker.delegate = self;//设置拍照后的图片可被编辑picker.allowsEditing = YES;picker.sourceType = sourceType;[self presentViewController:picker animated:YES completion:nil];}else{NSLog(@"模拟其中无法打开照相机,请在真机中使用");}
}
//选取本地图片
- (void)localPhoto{UIImagePickerController *picker = [[UIImagePickerController alloc] init];picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;picker.delegate = self;picker.allowsEditing = YES;[self presentViewController:picker animated:YES completion:nil];
}
//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{NSString *type = [info objectForKey:UIImagePickerControllerMediaType];//当选择的类型是图片if ([type isEqualToString:@"public.image"]){//先把图片转成NSDataUIImage* 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:@"/image.png"] contents:data attributes:nil];//得到选择后沙盒中图片的完整路径self.filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,  @"/image.png"];//关闭相册界面[picker dismissViewControllerAnimated:YES completion:nil];//创建一个选择后图片的小图标放在下方//类似微薄选择图后的效果UIImageView *smallimage = [[UIImageView alloc] initWithFrame:CGRectMake(110, 390, 75, 40)];smallimage.image = image;//加在视图中[self.imageSelectBtn removeFromSuperview];//移除button[self.view addSubview:smallimage];}}
<pre name="code" class="objc">- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{NSLog(@"您取消了选择图片");[picker dismissViewControllerAnimated:YES completion:nil];
}

调去系统照相机或者从本地相册获取图片.相关推荐

  1. Android 调用系统打开相机,打开相册获取图片路径

    我们在开发中经常遇到一些功能需要调取系统相机拍照获取图片,或者有的时候直接打开图库获取图片,那我们怎么获取呢,今天分享下, 第一步,打开相机 public static final int CAMER ...

  2. iOS 根据图片URL从本地相册获取图片

    最近做一个聊天的项目,需要发送图片后读取本地图片显示到列表里.刚开始的时候,天真的认为可以用SDWebImage直接加载,然后并不能行. 于是在网上搜了搜,如何根据从相册获取的UIImagePicke ...

  3. android从本地相册获取图片uri三星手机适配问题

    启动系统相册intent Intent intentFromGallery = new Intent(); if (android.os.Build.VERSION.SDK_INT >= and ...

  4. iOS打开照相机与本地相册选择图片

    最近正好项目里面要集成"打开照相机与本地相册选择图片"的功能,今天就在这边给大家写一个演示程序:打开相机拍摄后或者在相册中选择一张照片,然后将它显示在界面上.好了废话不多说,因为比 ...

  5. iOS 打开照相机与本地相册选择图片

    最近正好项目里面要集成"打开照相机与本地相册选择图片"的功能,今天就在这边给大家写一个演示程序:打开相机拍摄后或者在相册中选择一张照片,然后将它显示在界面上.好了废话不多说,因为比 ...

  6. uniapp 本地相册选择图片

    标题 uniapp 本地相册选择图片 export default {data(){return {imageLists:[]}} }methods:{delete(index){ // 获取该数组的 ...

  7. uni-app 从本地相册选择图片或使用相机拍照

    终于弄懂这个选择图片的流程,删除了很多不需要的地方,最终得到的就是这个版本 页面部分 <view>上传图片 {{imageList.length}}/{{count}}</view& ...

  8. 本地相册中图片水印如何去除【图片去水印工具】

    地址:本地相册中图片水印如何去除https://www.shuiyinyun.com/  照片去水印效果 操作步骤: 点击"立即下载"按钮,下载.安装并运行水印云: 点击首页的&q ...

  9. 从相机相册获取图片裁剪后用于评论晒图或更换背景图

    这是我人生中写的第一篇博客,是否要纪念一下这一刻(2016.09.01 16:52).其实关于写博客,老早就有这种写法,首先觉得他能够帮我总结我学到的和用过的技术,其次还能帮助那些和我有一样需求的人, ...

最新文章

  1. C++编程进阶3(如何写出正确的operator=、operator运算符的返回值以及是否应该是成员函数的讨论)
  2. 前端小白的 docker 配置nginx踩坑之旅
  3. hive(1)——用mysql进行元数据存储
  4. docker修改容器映射的端口_解密 Docker 挂载文件,宿主机修改后容器里文件没有修改...
  5. [文摘]上软解散相关
  6. html下拉框设置默认值_html 里select 下拉列表中设置默认值怎么写
  7. html简单网页代码 案例_HTML+CSS静态网页练习案例(转动的八卦图)
  8. 【渝粤教育】国家开放大学2018年秋季 2720T实用管理基础 参考试题
  9. 研发/项目计划管理表格
  10. 最方便的办法下载bilibili视频
  11. 手把手教你制作gif动图,一分钟轻松学会
  12. vsode 编译报错:main.c:4:10: fatal error: iostream: 没有那个文件或目录
  13. 1080i、720p、1080p、N制、P制、帧率、高清电视、全高清
  14. 玩转Fasttext
  15. 做有责任的企业!拉卡拉获“2018年度责任品牌奖”
  16. poi-tl填充动态word表格数据
  17. Linux下打开word等office文件(openoffice安装)
  18. 数据库外键的作用,以及和主键的区别
  19. ElasticSearch常用插件整理
  20. 大数据领域就业和发展指南

热门文章

  1. websocketapp保活,来自阿里巴巴佛系Android程序员的指南,写给正在求职的Android开发
  2. python二元函数求导_python实现GA求二元函数最大值(来自知乎)
  3. 超详细配置教程,搭建Windows深度学习环境
  4. 坚果Pro真机已确认外观设计!
  5. 百度AI识别声音(sing)
  6. python CV2中shape和resize返回值和参数的区别
  7. hexo-admin快速发布博客
  8. 升级ipython_Pythonipython的安装于升级 - lwgarmstrong
  9. 【ESP32最全学习笔记(基础篇)——9.ESP32 深度睡眠模式】
  10. Part I 空气曲棍球 Chapter8(8.2 Adding a Geometry Class)