IOS 调用照相机、相册功能

2024-05-31 14:48

本文主要是介绍IOS 调用照相机、相册功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先需要导入<AssetsLibrary/AssetsLibrary.h>、<Photos/Photos.h>这两个框架,然后遵循UIImagePickerControllerDelegate。


调用相册

#pragma mark 选择图片
- (void)selectPhoto
{if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusRestricted || [PHPhotoLibrary authorizationStatus] == AVAuthorizationStatusDenied) {NSString *appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleDisplayName"];if (!appName) appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleName"];NSString *info = [NSString stringWithFormat:@"请在%@的\"设置-隐私-照片\"选项中,\r允许%@访问你的手机相册。",[UIDevice currentDevice].model,appName];UIAlertView *showAlert = [[UIAlertView alloc] initWithTitle:nil message:info delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil];[showAlert show];}else {//1.首先判断照片源是否可用if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {//2.实例化UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];//2.1设置照片源imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//2.2是否允许修改imagePicker.allowsEditing = YES;//2.3设置代理imagePicker.delegate = self;//2.4显示控制器[self presentViewController:imagePicker animated:YES completion:nil];}}
}

调用照相机

#pragma mark 拍照
- (void)takePhoto
{AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {NSString *appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleDisplayName"];if (!appName) appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleName"];NSString *info = [NSString stringWithFormat:@"请在%@的\"设置-隐私-相机\"选项中,\r允许%@访问你的照相机。",[UIDevice currentDevice].model,appName];UIAlertView *showAlert = [[UIAlertView alloc] initWithTitle:nil message:info delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil];[showAlert show];} else { // 调用相机if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;imagePicker.allowsEditing = YES;imagePicker.delegate = self;[self presentViewController:imagePicker animated:YES completion:nil];}}
}

代理方法

#pragma mark - UIImagePickerController代理
#pragma mark 完成
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{UIImage *image = info[@"UIImagePickerControllerEditedImage"];[picker dismissViewControllerAnimated:YES completion:nil];
}#pragma mark 取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{[picker dismissViewControllerAnimated:YES completion:nil];
}



这篇关于IOS 调用照相机、相册功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

一分钟带你上手Python调用DeepSeek的API

《一分钟带你上手Python调用DeepSeek的API》最近DeepSeek非常火,作为一枚对前言技术非常关注的程序员来说,自然都想对接DeepSeek的API来体验一把,下面小编就来为大家介绍一下... 目录前言免费体验API-Key申请首次调用API基本概念最小单元推理模型智能体自定义界面总结前言最

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav

SpringBoot整合DeepSeek实现AI对话功能

《SpringBoot整合DeepSeek实现AI对话功能》本文介绍了如何在SpringBoot项目中整合DeepSeekAPI和本地私有化部署DeepSeekR1模型,通过SpringAI框架简化了... 目录Spring AI版本依赖整合DeepSeek API key整合本地化部署的DeepSeek

Python实现多路视频多窗口播放功能

《Python实现多路视频多窗口播放功能》这篇文章主要为大家详细介绍了Python实现多路视频多窗口播放功能的相关知识,文中的示例代码讲解详细,有需要的小伙伴可以跟随小编一起学习一下... 目录一、python实现多路视频播放功能二、代码实现三、打包代码实现总结一、python实现多路视频播放功能服务端开

css实现图片旋转功能

《css实现图片旋转功能》:本文主要介绍了四种CSS变换效果:图片旋转90度、水平翻转、垂直翻转,并附带了相应的代码示例,详细内容请阅读本文,希望能对你有所帮助... 一 css实现图片旋转90度.icon{ -moz-transform:rotate(-90deg); -webkit-transfo

redis防止短信恶意调用的实现

《redis防止短信恶意调用的实现》本文主要介绍了在场景登录或注册接口中使用短信验证码时遇到的恶意调用问题,并通过使用Redis分布式锁来解决,具有一定的参考价值,感兴趣的可以了解一下... 目录1.场景2.排查3.解决方案3.1 Redis锁实现3.2 方法调用1.场景登录或注册接口中,使用短信验证码场

使用C/C++调用libcurl调试消息的方式

《使用C/C++调用libcurl调试消息的方式》在使用C/C++调用libcurl进行HTTP请求时,有时我们需要查看请求的/应答消息的内容(包括请求头和请求体)以方便调试,libcurl提供了多种... 目录1. libcurl 调试工具简介2. 输出请求消息使用 CURLOPT_VERBOSE使用 C

C语言小项目实战之通讯录功能

《C语言小项目实战之通讯录功能》:本文主要介绍如何设计和实现一个简单的通讯录管理系统,包括联系人信息的存储、增加、删除、查找、修改和排序等功能,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录功能介绍:添加联系人模块显示联系人模块删除联系人模块查找联系人模块修改联系人模块排序联系人模块源代码如下