【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];
}

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

相关推荐
高升说1 小时前
光与影的邂逅-09-太亮的地方
数码相机·深度相机
_瑞5 小时前
读懂 iOS 的线程调用栈
ios·编译原理·汇编语言
末代iOS程序员华仔8 小时前
iOS 语聊直播/聊天APP4.3条例被拒解决
flutter·ios·swift
高升说8 小时前
光与影的邂逅-08-丢失的光
数码相机·深度相机
2601_965798471 天前
Build a Fast, High-Ranking Restaurant Website with Rolanda Theme
开发语言·ios·swift
极客互动API1 天前
企业微信 iPad 协议消息接口开发:文本 / 图片 / 群发消息的统一封装
人工智能·ios·微信·机器人·企业微信·ipad
我血条子呢1 天前
iOS 软键盘遮挡底部输入框解决报告
ios
CVer儿1 天前
ras转为lps坐标系
数码相机
思盛iOS签名上架1 天前
为什么 iOS 需要签名?未签名 App 无法安装的底层逻辑
ios
恒锐丰小瑞2 天前
率能SS6810R 40V/1A/双通道H桥驱动芯片,PWM电流控制与混合衰减模式(eTSSOP20),用于POS打印机/安防相机/机器人
单片机·嵌入式硬件·数码相机·机器人