iOS中 读取相册,调用系统相机 技术分享
来源:互联网 发布:索尼如何用网络看电视 编辑:程序博客网 时间:2024/05/29 10:52
技术内容:分别读取相册以及调取相机,将图片显示到imageView上
布局:
1.创建imageView 和 button 并为button一个关联pickerImage的事件
1 - (void)pickerImage:(UIButton *)button{
2 //添加ActionSheet控件,提示选项框,调出相机或拍摄图片
3 //第一个参数:是行为列表的标题 一般为空
4 //第二个参数:遵循代理
5 //第三个参数:取消这个操作按钮上 显示的文字
6 //第四个参数:destructive 破坏性的, 毁灭性的 自己理解吧 反正我写的是拍照,执行操作的意思
7 //第五个参数:从相册选取图片
8 UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"从相册选择图片", nil nil];
9 //在当前界面显示actionSheet对象
10 [actionSheet showInView:self.view];
11 [actionSheet release];
12 }
4.实现action代理协议中的方法
[objc] view plaincopy
1 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
2 switch (buttonIndex) {
3 case 0:
4 //调用系统相机,拍照
5 [self pickerPictureFromCamera];
6 break;
7 case 1:
8 [self pickerPictureFromPhotosAlbum];
9 default:
10 break;
11 }
12 }
4.1从摄像头获取图片
[objc] view plaincopy
1 - (void)pickerPictureFromCamera{
2
3 //判断前摄像头是否可用,如果不可用的话,用后摄像头。如果后摄像头也不可用的话用手机图片库
4 //判断前置摄像头是否可用
5 if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
6 NSLog(@"用前置摄像头");
7
8 self.imagePC = [[UIImagePickerController alloc]init];
9 self.imagePC.cameraDevice = UIImagePickerControllerCameraDeviceFront;
10 [self.imagePC release];
11 //判断后置摄像头是否可用
12 }else if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]){
13 NSLog(@"用后置摄像头");
14
15 self.imagePC = [[UIImagePickerController alloc]init];
16 self.imagePC.cameraDevice = UIImagePickerControllerCameraDeviceRear;
17 [self.imagePC release];
18 //两者都不行的话,从手机相册调取照片
19 }else{
20 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"调用摄像头失败" message:@"请从手机相册中选取照片" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];
21 [alertView show];
22 [alertView release];
23 }
24
25 //初始化图片控制器对象
26 UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
27 //sourceType资源样式
28 //设置图片选择器选择图片的样式
29 imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
30 //设置图片是否允许编辑
31 imagePicker.allowsEditing = YES;
32 //设置图片选择器代理对象为这个视图控制器
33 imagePicker.delegate = self;
34 //把相机推出来 模态
35 [self presentViewController:imagePicker animated:YES completion:nil];
36 //释放
37 [imagePicker release];
38
39 }
4.2从手机的图片库获取图片
[objc] view plaincopy
1 - (void)pickerPictureFromPhotosAlbum{
2 //初始化图片控制器对象
3 UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
4 //sourceType资源样式
5 //设置图片选择器选择图片的样式
6 imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
7 //设置图片是否允许编辑
8 imagePicker.allowsEditing = YES;
9 //设置图片选择器代理对象为这个视图控制器
10 imagePicker.delegate = self;
11 //把选择控制器推出来 模态
12 [self presentViewController:imagePicker animated:YES completion:nil];
13 //释放
14 [imagePicker release];
15 }
5.将选取好的照片保存到详情页的方法
[objc] view plaincopy
1 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
2 //以相册作为字典,从中取出照片
3 self.aImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
4 //把选取框模态回去
5 [self dismissViewControllerAnimated:YES completion:nil];
6 }
布局:
1.创建imageView 和 button 并为button一个关联pickerImage的事件
1 - (void)pickerImage:(UIButton *)button{
2 //添加ActionSheet控件,提示选项框,调出相机或拍摄图片
3 //第一个参数:是行为列表的标题 一般为空
4 //第二个参数:遵循代理
5 //第三个参数:取消这个操作按钮上 显示的文字
6 //第四个参数:destructive 破坏性的, 毁灭性的 自己理解吧 反正我写的是拍照,执行操作的意思
7 //第五个参数:从相册选取图片
8 UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"从相册选择图片", nil nil];
9 //在当前界面显示actionSheet对象
10 [actionSheet showInView:self.view];
11 [actionSheet release];
12 }
4.实现action代理协议中的方法
[objc] view plaincopy
1 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
2 switch (buttonIndex) {
3 case 0:
4 //调用系统相机,拍照
5 [self pickerPictureFromCamera];
6 break;
7 case 1:
8 [self pickerPictureFromPhotosAlbum];
9 default:
10 break;
11 }
12 }
4.1从摄像头获取图片
[objc] view plaincopy
1 - (void)pickerPictureFromCamera{
2
3 //判断前摄像头是否可用,如果不可用的话,用后摄像头。如果后摄像头也不可用的话用手机图片库
4 //判断前置摄像头是否可用
5 if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
6 NSLog(@"用前置摄像头");
7
8 self.imagePC = [[UIImagePickerController alloc]init];
9 self.imagePC.cameraDevice = UIImagePickerControllerCameraDeviceFront;
10 [self.imagePC release];
11 //判断后置摄像头是否可用
12 }else if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]){
13 NSLog(@"用后置摄像头");
14
15 self.imagePC = [[UIImagePickerController alloc]init];
16 self.imagePC.cameraDevice = UIImagePickerControllerCameraDeviceRear;
17 [self.imagePC release];
18 //两者都不行的话,从手机相册调取照片
19 }else{
20 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"调用摄像头失败" message:@"请从手机相册中选取照片" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];
21 [alertView show];
22 [alertView release];
23 }
24
25 //初始化图片控制器对象
26 UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
27 //sourceType资源样式
28 //设置图片选择器选择图片的样式
29 imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
30 //设置图片是否允许编辑
31 imagePicker.allowsEditing = YES;
32 //设置图片选择器代理对象为这个视图控制器
33 imagePicker.delegate = self;
34 //把相机推出来 模态
35 [self presentViewController:imagePicker animated:YES completion:nil];
36 //释放
37 [imagePicker release];
38
39 }
4.2从手机的图片库获取图片
[objc] view plaincopy
1 - (void)pickerPictureFromPhotosAlbum{
2 //初始化图片控制器对象
3 UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
4 //sourceType资源样式
5 //设置图片选择器选择图片的样式
6 imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
7 //设置图片是否允许编辑
8 imagePicker.allowsEditing = YES;
9 //设置图片选择器代理对象为这个视图控制器
10 imagePicker.delegate = self;
11 //把选择控制器推出来 模态
12 [self presentViewController:imagePicker animated:YES completion:nil];
13 //释放
14 [imagePicker release];
15 }
5.将选取好的照片保存到详情页的方法
[objc] view plaincopy
1 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
2 //以相册作为字典,从中取出照片
3 self.aImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
4 //把选取框模态回去
5 [self dismissViewControllerAnimated:YES completion:nil];
6 }
0 0
- iOS中 读取相册,调用系统相机 技术分享
- iOS中 读取相册,调用系统相机 技术分享
- iOS开发中调用系统相册、相机 显示中文标题
- ios开发调用系统相机相册
- iOS开发 调用系统相机和相册
- [iOS]调用系统相机,相册,闪光灯
- iOS学习: 系统相机 相册的调用
- iOS 调用系统相机和相册
- iOS中调用相机以及相册
- 调用系统相机相册
- 调用系统相册,相机
- 调用系统相机相册
- Unity与IOS交互,调用IOS系统相机和相册
- iOS调用系统相机,访问系统相册功能实现
- 关于iOS中简单实现调用系统相机及相册功能
- 关于iOS中简单实现调用系统相机及相册功能
- 关于iOS中简单实现调用系统相机及相册功能
- 关于iOS中简单实现调用系统相机及相册功能
- VLC播放RTSP视频延迟问题
- 利用Matlab拟合时序植被生长季曲线,并求解物候参数
- 错误:Uncaught TypeError: Cannot read property 'WebSocket' of undefined
- jquery日期控件My97DatePickerBeta
- spring3.1.0 jar包详解说明
- iOS中 读取相册,调用系统相机 技术分享
- Day7.1 模块化编程的概念
- IT公司面试手册提供最全的软件测试类面试题
- Mysql数据库 hibernate保存数据时,Mysql主键需设定自增,否则报错!(Field 'id' doesn't have a default value)
- 前端优化带来的思考,浅谈前端工程化
- win2012的VMQ好像是鸡肋
- bzoj2067: [Poi2004]SZN
- [2001]:计算两点间的距离(简单的题目)
- Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用