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

2024-01-20 19:48

本文主要是介绍iOS打开照相机与本地相册选择图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

首先,我们在头文件中添加需要用到的actionSheet控件,显示图片的UIImageView控件,并且加上所需要的协议

[objc]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ImagePickerViewController : UIViewController<UIImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate>  
  4.   
  5. @property (strongnonatomic) IBOutlet UIImageView *headImage;  
  6.   
  7. @property (strongnonatomicUIActionSheet *actionSheet;  
  8.   
  9. - (IBAction)clickPickImage:(id)sender;  
  10. @end  

通过点击我设置在界面中的按钮来呼出actionSheet控件,来选择相应的操作拍照或是在相册中选择相片,代码如下:

[objc]  view plain copy
在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  ImagePickerViewController.m  
  3. //  testAuto  
  4. //  
  5. //  Created by silicon on 15/5/9.  
  6. //  Copyright (c) 2015年 silicon. All rights reserved.  
  7. //  
  8.   
  9. #import "ImagePickerViewController.h"  
  10.   
  11. @interface ImagePickerViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation ImagePickerViewController  
  16.   
  17. @synthesize actionSheet = _actionSheet;  
  18.   
  19. - (void)viewDidLoad {  
  20.     [super viewDidLoad];  
  21.     // Do any additional setup after loading the view from its nib.  
  22.       
  23. }  
  24.   
  25. - (void)didReceiveMemoryWarning {  
  26.     [super didReceiveMemoryWarning];  
  27.     // Dispose of any resources that can be recreated.  
  28. }  
  29.   
  30.   
  31. /** 
  32.  @ 调用ActionSheet 
  33.  */  
  34. - (void)callActionSheetFunc{  
  35.     if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){  
  36.         self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照"@"从相册选择", nil nil];  
  37.     }else{  
  38.         self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil nil];  
  39.     }  
  40.       
  41.     self.actionSheet.tag = 1000;  
  42.     [self.actionSheet showInView:self.view];  
  43. }  
  44.   
  45. // Called when a button is clicked. The view will be automatically dismissed after this call returns  
  46. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{  
  47.     if (actionSheet.tag == 1000) {  
  48.         NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  49.         // 判断是否支持相机  
  50.         if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {  
  51.             switch (buttonIndex) {  
  52.                 case 0:  
  53.                     //来源:相机  
  54.                     sourceType = UIImagePickerControllerSourceTypeCamera;  
  55.                     break;  
  56.                 case 1:  
  57.                     //来源:相册  
  58.                     sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  59.                     break;  
  60.                 case 2:  
  61.                     return;  
  62.             }  
  63.         }  
  64.         else {  
  65.             if (buttonIndex == 2) {  
  66.                 return;  
  67.             } else {  
  68.                 sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;  
  69.             }  
  70.         }  
  71.         // 跳转到相机或相册页面  
  72.         UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];  
  73.         imagePickerController.delegate = self;  
  74.         imagePickerController.allowsEditing = YES;  
  75.         imagePickerController.sourceType = sourceType;  
  76.           
  77.         [self presentViewController:imagePickerController animated:YES completion:^{  
  78.           
  79.         }];  
  80.     }  
  81. }  
  82.   
  83. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  
  84. {  
  85.     [picker dismissViewControllerAnimated:YES completion:^{  
  86.       
  87.     }];  
  88.       
  89.     UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];  
  90.     self.headImage.image = image;  
  91. }  
  92.   
  93. /* 
  94. #pragma mark - Navigation 
  95.  
  96. // In a storyboard-based application, you will often want to do a little preparation before navigation 
  97. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
  98.     // Get the new view controller using [segue destinationViewController]. 
  99.     // Pass the selected object to the new view controller. 
  100. } 
  101. */  
  102.   
  103. - (IBAction)clickPickImage:(id)sender {  
  104.       
  105.     [self callActionSheetFunc];  
  106. }  
  107. @end  

代码比较简单,也容易理解,运行的效果如下:













这篇关于iOS打开照相机与本地相册选择图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/627134

相关文章

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

Python利用PIL进行图片压缩

《Python利用PIL进行图片压缩》有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所以本文为大家介绍了Python中图片压缩的方法,需要的可以参考下... 有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所有可以对文件中的图

java获取图片的大小、宽度、高度方式

《java获取图片的大小、宽度、高度方式》文章介绍了如何将File对象转换为MultipartFile对象的过程,并分享了个人经验,希望能为读者提供参考... 目China编程录Java获取图片的大小、宽度、高度File对象(该对象里面是图片)MultipartFile对象(该对象里面是图片)总结java获取图片

一文教你使用Python实现本地分页

《一文教你使用Python实现本地分页》这篇文章主要为大家详细介绍了Python如何实现本地分页的算法,主要针对二级数据结构,文中的示例代码简洁易懂,有需要的小伙伴可以了解下... 在项目开发的过程中,遇到分页的第一页就展示大量的数据,导致前端列表加载展示的速度慢,所以需要在本地加入分页处理,把所有数据先放

Java实战之自助进行多张图片合成拼接

《Java实战之自助进行多张图片合成拼接》在当今数字化时代,图像处理技术在各个领域都发挥着至关重要的作用,本文为大家详细介绍了如何使用Java实现多张图片合成拼接,需要的可以了解下... 目录前言一、图片合成需求描述二、图片合成设计与实现1、编程语言2、基础数据准备3、图片合成流程4、图片合成实现三、总结前

本地搭建DeepSeek-R1、WebUI的完整过程及访问

《本地搭建DeepSeek-R1、WebUI的完整过程及访问》:本文主要介绍本地搭建DeepSeek-R1、WebUI的完整过程及访问的相关资料,DeepSeek-R1是一个开源的人工智能平台,主... 目录背景       搭建准备基础概念搭建过程访问对话测试总结背景       最近几年,人工智能技术

如何在本地部署 DeepSeek Janus Pro 文生图大模型

《如何在本地部署DeepSeekJanusPro文生图大模型》DeepSeekJanusPro模型在本地成功部署,支持图片理解和文生图功能,通过Gradio界面进行交互,展示了其强大的多模态处... 目录什么是 Janus Pro1. 安装 conda2. 创建 python 虚拟环境3. 克隆 janus

本地私有化部署DeepSeek模型的详细教程

《本地私有化部署DeepSeek模型的详细教程》DeepSeek模型是一种强大的语言模型,本地私有化部署可以让用户在自己的环境中安全、高效地使用该模型,避免数据传输到外部带来的安全风险,同时也能根据自... 目录一、引言二、环境准备(一)硬件要求(二)软件要求(三)创建虚拟环境三、安装依赖库四、获取 Dee

使用Python实现图片和base64转换工具

《使用Python实现图片和base64转换工具》这篇文章主要为大家详细介绍了如何使用Python中的base64模块编写一个工具,可以实现图片和Base64编码之间的转换,感兴趣的小伙伴可以了解下... 简介使用python的base64模块来实现图片和Base64编码之间的转换。可以将图片转换为Bas