iOS--UIimagePickerController

news/2024/7/20 5:41:45 标签: ios, cocoa, 数码相机

iOS--UIimagePickerController

  • 前记
    • UiimagePickerControlller
      • viewdidload
      • presstap
      • gettoCamera
      • gettoPhoto
      • 切换头像
      • UIimagePickerController常用代理方法
    • 演示图片

前记

写完知乎日报之后,不知道该学些什么东西,但总的和以后写的项目有关,所以看学长的博客先学习一项相机和相册的调用

UiimagePickerControlller

这是iOS中自带得一个UI类,可以实现对相机及相册的调用和对媒体的处理。

UIImagePickerController 是 iOS 开发中的一个内置视图控制器,用于方便地实现图像和视频的选择和拍摄功能。它提供了一个用户界面,允许用户从设备的相册库中选择照片或视频,或者使用设备的摄像头进行拍摄。
使用 UIImagePickerController 可以实现以下功能:
从相册中选择照片:用户可以浏览设备相册中的照片,并选择其中的一张照片作为所选项。
从相册中选择视频:用户可以浏览设备相册中的视频,并选择其中的一个视频作为所选项。
拍摄照片:用户可以使用设备的摄像头拍摄照片,并将其作为所选项。
拍摄视频:用户可以使用设备的摄像头录制视频,并将其作为所选项。
提供图像编辑功能:UIImagePickerController 还提供了一些简单的图像编辑功能,如裁剪和调整大小等。

我访问他的定义了解到它是UINavigationController的子类,也就是说它是一个Controller,所以基本的操作就是presentviewcontroller ;至于功能是调用相机还是相册由属性决定 ;

viewdidload

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.headimageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"biaoqing.png"] ];
    self.headimageview.userInteractionEnabled = YES ;
    UITapGestureRecognizer* tapgesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(presstap)] ;
    self.headimageview.frame = CGRectMake(100, 100, 100, 100) ;
    [self.headimageview addGestureRecognizer:tapgesture] ;//通过手势来添加imageview的点击函数
    [self.view addSubview:self.headimageview] ;
}

presstap

- (void)presstap {
    UIAlertController* alertion = [UIAlertController alertControllerWithTitle:@"please" message:@"select" preferredStyle:UIAlertControllerStyleActionSheet] ;
    UIAlertAction* camera = [UIAlertAction actionWithTitle:@"camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self gettoCamera] ;
    }] ;
    UIAlertAction* photo = [UIAlertAction actionWithTitle:@"photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self gettoPhoto] ;
    }] ;
    UIAlertAction* cancal = [UIAlertAction actionWithTitle:@"cancal" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }] ;
    [alertion addAction:camera] ;
    [alertion addAction:photo] ;
    [alertion addAction:cancal] ;
    
    [self presentViewController:alertion animated:YES completion:nil] ;
    
}

gettoCamera

- (void)gettoCamera {
//初始化Controller
    self.imagepickercontroller = [[UIImagePickerController alloc] init] ;
    //设置代理
    self.imagepickercontroller.delegate = self ;
    //设置sourceType来决定访问相机还是相册
    self.imagepickercontroller.sourceType = UIImagePickerControllerSourceTypeCamera ;
    //设置改Controller为全屏
    self.imagepickercontroller.modalPresentationStyle = UIModalPresentationFullScreen ;
    //self.imagepickercontroller.cameraCaptureMode 是 UIImagePickerController 的一个属性,用于设置相机的捕捉模式。在你提供的代码中,设置为 UIImagePickerControllerCameraCaptureModePhoto 表示将相机设置为仅拍摄照片的模式。
    self.imagepickercontroller.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto ;
    //跳转
    [self presentViewController:self.imagepickercontroller animated:YES completion:nil] ;
}

gettoPhoto

- (void)gettoPhoto {
    //初始化Controller
    self.imagepickercontroller = [[UIImagePickerController alloc] init] ;
    //设置代理
    self.imagepickercontroller.delegate = self ;
    //设置相册照片允许选择
    self.imagepickercontroller.allowsEditing = YES ;
    //设置sourceType访问相册
    self.imagepickercontroller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary ;
    [self presentViewController:self.imagepickercontroller animated:YES completion:nil] ;
}

切换头像

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    //切换头像
    [self.imagepickercontroller dismissViewControllerAnimated:YES completion:nil] ;
    UIImage* image = [info valueForKey:UIImagePickerControllerEditedImage] ;
    self.headimageview.image = image ;
 }

注意一下,最后一个方法是代理方法,要实现代理才能使用,

@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) UIImagePickerController* imagepickercontroller ;
@property (nonatomic, strong) UIImageView* headimageview ;

@end

UIimagePickerController常用代理方法

1.imagePickerController(_:didFinishPickingMediaWithInfo:):
当用户选择或拍摄媒体后,该方法会被调用。你可以在该方法中获取选择的图像或视频,并对其进行处理。

2.imagePickerControllerDidCancel(_):
当用户取消选择或拍摄媒体时,该方法会被调用。你可以在该方法中执行相应的取消操作。

演示图片

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


http://www.niftyadmin.cn/n/5226389.html

相关文章

Apache HTTP 安装和配置下载

文章目录 配置和下载安装服务反向代理安全设置 配置和下载 Apache HTTP 安装和配置下载 https://www.apachelounge.com/download/ 下载完成后解压即可 Apache配置文件 Apache24\conf\httpd.conf修改apache安装路径 Define SRVROOT 安装目录(当前位置)DocumentRoot 指定你…

OpenHarmony 4.0 Release 编译及报错

1、环境准备 安装下面这三东西&#xff0c;是为了下载 Harmony 源码 sudo apt install curl sudo apt install python3-pip sudo apt install git-lfs 安装下面这五个东西&#xff0c;是为了解决编译到最后报错(头铁不信的&#xff0c;你可以试试&#xff0c;等最后再安装) …

【面试】typescript

目录 为什么用TypeScript&#xff1f; TS和JS的区别 控制类成员可见性的访问关键字&#xff1f; public protected&#xff09;&#xff0c;该类及其子类都可以访问它们。 但是该类的实例无法访问。 私有&#xff08;private&#xff09;&#xff0c;只有类的成员可以访问…

uniappios请求打开麦克风 uniapp发起请求

第一种 ajax请求方式 uni.request(OBJECT) 参数名类型必填默认值说明平台差异说明urlString是开发者服务器接口地址dataObject/String/ArrayBuffer否请求的参数App(自定义组件编译模式)不支持ArrayBuffer类型headerObject否设置请求的 header,header 中不能设置 Referer。…

Python内置函数与标准库函数的详细解读

一、内置函数与标准库函数的区分 Python 解释器自带的函数叫做内置函数&#xff0c;这些函数可以直接使用&#xff0c;不需要导入某个模块。 Python 解释器也是一个程序&#xff0c;它给用户提供了一些常用功能&#xff0c;并给它们起了独一无二的名字&#xff0c;这些常用功能…

基于ssm的汽车论坛管理系统设计与实现

基于ssm的汽车论坛管理系统设计与实现 摘要&#xff1a;信息化社会内需要与之针对性的信息获取途径&#xff0c;但是途径的扩展基本上为人们所努力的方向&#xff0c;由于站在的角度存在偏差&#xff0c;人们经常能够获得不同类型信息&#xff0c;这也是技术最为难以攻克的课题…

C# 使用 Fody 监控方法执行时间

写在前面 在做性能调优的时候&#xff0c;经常需要跟踪具体方法的执行时间&#xff1b;通过插入Stopwatch的方案对代码的侵入性太高了&#xff0c;所以引入了 MethodTimer.Fody 类库&#xff0c;采用编译时注入的方式给方法动态加上Stopwatch 跟踪代码&#xff0c;只需要在目标…

《opencv实用探索·四》Mat图像数据类型转换和归一化显示

一种数据类型转为另一种数据类型&#xff0c;不改变图像大小&#xff0c;但每个像素值可能会变 src.convertTo(dst, type, scale, shift);Scale和shitf默认为0&#xff08;这两个参数也相当于对比度和亮度&#xff09; 现在有个8位图像&#xff0c;把8位转成32位 可以看到像素…