【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];
}
后续还会根据项目需要学习心新的知识。