【iOS-UIImagePickerController访问相机和相册】

【iOS-UIImagePickerController访问相机和相册】

  • [一. UIImagePickerController的介绍](#一. UIImagePickerController的介绍)
    • [1 . UIImagePickerController的作用](#1 . UIImagePickerController的作用)
    • [2 . UIImagePickerController的功能](#2 . UIImagePickerController的功能)
  • [二 . UIImagePickerController的测试程序](#二 . UIImagePickerController的测试程序)

一. UIImagePickerController的介绍

1 . UIImagePickerController的作用

UIImagePickerController是iOS平台上的一个类,用于在应用程序中访问设备的照片库、相机和视频录制功能。它提供了一个用户界面,使用户可以从设备的媒体库中选择照片或视频,或者使用设备的摄像头拍摄照片或录制视频。在这里我们先只介绍一下访问相机和相册这两个功能。

2 . UIImagePickerController的功能

(1)访问照片库:使用sourceType属性设置为UIImagePickerControllerSourceTypePhotoLibrary,用户可以从设备的照片库中选择照片或视频。

(2)调用相机拍摄照片:使用sourceType属性设置为UIImagePickerControllerSourceTypeCamera,用户可以使用设备的摄像头拍摄照片。

(3)录制视频:使用sourceType属性设置为UIImagePickerControllerSourceTypeCamera,并将mediaTypes属性设置为支持视频录制的类型,用户可以使用设备的摄像头录制视频。

二 . UIImagePickerController的测试程序

中间的黄色区域是可以显示图片的区域,可以通过访问系统的相册的照片来改变该位置的图片。

上面这张图片就是通过访问相册改变的图片。

具体的代码步骤如下:

首先,往视图控制器上面添加导航栏,往导航栏上面添加一张图,并且给图片加上点击事件,并且添加一个视图区域(黄色区域),通过点击事件来选择怎样改变黄色区域的视图。

objectivec 复制代码
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =  UIColor.whiteColor;
    // Do any additional setup after loading the view.
    
    //添加导航栏的方法调用
    [self addNavigation];
    
    //添加黄视图区域
    self.headImageview = [[UIImageView alloc] initWithFrame:CGRectMake(120, 200, 153, 153)];
    self.headImageview.backgroundColor = UIColor.yellowColor;
    [self.view addSubview:self.headImageview];
}

- (void) addNavigation {
    //手动设置的导航栏
    UINavigationBarAppearance* apperance = [UINavigationBarAppearance new];
    [apperance configureWithOpaqueBackground];
    apperance.backgroundColor = [UIColor colorWithRed:55/255.0 green: 130/255.0 blue:223/255.0 alpha:1.0];
    apperance.shadowColor = [UIColor clearColor];
    self.navigationController.navigationBar.standardAppearance = apperance;
    self.navigationController.navigationBar.scrollEdgeAppearance = self.navigationController.navigationBar.standardAppearance;

    //设置一个自定义的视图添加到导航栏上。
    UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 395, 44)];
    view1.center = self.navigationController.navigationBar.center;

    self.avatar = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xiangji.png"]];
    self.avatar.frame = CGRectMake(310, -5, 44, 44);
    self.avatar.userInteractionEnabled = YES;
    [view1 addSubview: self.avatar];

	//添加点击事件
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(catchImage)];
    [self.avatar addGestureRecognizer:tap];

    //将 view1 设置为当前视图控制器的导航栏标题视图
    self.navigationItem.titleView = view1;
}

然后在点击事件中实现是想要通过相机还是相册来改变视图。

objectivec 复制代码
- (void) catchImage {
    self.imagePickController = [[UIImagePickerController alloc] init];
    self.imagePickController.delegate = self;
    self.imagePickController.allowsEditing = YES;
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction* camera = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self toCamera];
    }];
    
    UIAlertAction* album = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self toAlbum];
    }];
    
    UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }];
    
    [alert addAction: camera];
    [alert addAction: album];
    [alert addAction: cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void) toCamera {
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
       self.imagePickController.sourceType = UIImagePickerControllerSourceTypeCamera;
       self.imagePickController.modalPresentationStyle = UIModalPresentationFullScreen;
       [self presentViewController:self.imagePickController animated:YES completion:nil];
    } else {
        NSLog(@"无法打开相机");
    }
    
}

- (void) toAlbum  {
    self.imagePickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    self.imagePickController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:self.imagePickController animated:YES completion:nil];
}

但是要注意的是,模拟机是无法打开相机的,所以就会出现== Thread 1: "Source type 1 not available"==,然后程序就会崩溃。

最后就是通过一些协议方法来实现其他的功能。

imagePickerController:(UIImagePickerController *)picker是UIImagePickerController提供的三个代理方法之一。

objectivec 复制代码
// 取消选择,点击界面中的取消(Cancel)按钮时触发
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
 
}
 
// 选择完成,点击界面中的某个图片或者选择(Choose)按钮时触发
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.headImageview.image = image;
    [picker dismissViewControllerAnimated:YES completion:nil];
}

后续还会根据项目需要学习心新的知识。

相关推荐
文弱_书生17 小时前
关于对鱼眼相机图片进行畸变校正的两种思路
数码相机·鱼眼畸变校正·传统几何方法·深度学习方法
疾风铸境17 小时前
qt+halcon开发相机拍照软件步骤
数码相机·qt·halcon·拍照
m0_6410310518 小时前
在选择iOS代签服务前,你必须了解的三大安全风险
ios
开开心心loky19 小时前
[iOS] push 和 present Controller 的区别
ui·ios·objective-c·cocoa
白玉cfc1 天前
【iOS】push,pop和present,dismiss
macos·ios·cocoa
低调小一1 天前
iOS 开发入门指南-HelloWorld
ios
2501_915918411 天前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
用户870568130451 天前
iOS 异步渲染:从 CALayer 切入的实现与优化
ios
北岛三生1 天前
ISP(图像信号处理器)
图像处理·数码相机·测试工具·计算机视觉·测试用例·模块测试
兔子不吃草~2 天前
鱼眼相机模型
数码相机