您的位置:首页 > 移动开发 > IOS开发

iOS中 读取相册,调用系统相机 技术分享

2015-10-13 19:21 531 查看
技术内容:分别读取相册以及调取相机,将图片显示到imageView上

布局:

1.创建imageView 和 button 并为button一个关联pickerImage的事件

<div style="text-align: left;"><span style="font-family: Helvetica; -webkit-text-stroke-width: initial;"> self.aImageView = [[UIImageView alloc]initWithFrame:CGRectMake(60, 100, 200, 200)];</span></div> self.aImageView.backgroundColor = [UIColor redColor];
self.aImageView.userInteractionEnabled = YES;

self.aButton = [[UIButton alloc]initWithFrame:CGRectMake(98, 350, 125, 25)];
self.aButton.backgroundColor = [UIColor blueColor];
[self.aButton addTarget:self action:@selector(pickerImage:) forControlEvents:(UIControlEventTouchUpInside)];
[self.aButton setTitle:@"选择图像" forState:(UIControlStateNormal)];

[self.view addSubview:self.aButton];
[self.view addSubview:self.aImageView];
[self.aButton release];
[self.aImageView release];

[self addTapGestureOnImageView];

2.因为有的场景需要直接点击图片更换别的图片,所以在imageView上添加轻拍动作

- (void)addTapGestureOnImageView{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pickerImage:)];
[self.aImageView addGestureRecognizer:tap];
[tap release];
}

3.实现轻拍动作中方法

- (void)pickerImage:(UIButton *)button{
//添加ActionSheet控件,提示选项框,调出相机或拍摄图片
//第一个参数:是行为列表的标题 一般为空
//第二个参数:遵循代理
//第三个参数:取消这个操作按钮上 显示的文字
//第四个参数:destructive 破坏性的, 毁灭性的 自己理解吧 反正我写的是拍照,执行操作的意思
//第五个参数:从相册选取图片
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"从相册选择图片", nil];
//在当前界面显示actionSheet对象
[actionSheet showInView:self.view];
[actionSheet release];
}

4.实现action代理协议中的方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
//调用系统相机,拍照
[self pickerPictureFromCamera];
break;
case 1:
[self pickerPictureFromPhotosAlbum];
default:
break;
}
}


4.1从摄像头获取图片

- (void)pickerPictureFromCamera{

//判断前摄像头是否可用,如果不可用的话,用后摄像头。如果后摄像头也不可用的话用手机图片库
//判断前置摄像头是否可用
if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
NSLog(@"用前置摄像头");

self.imagePC = [[UIImagePickerController alloc]init];
self.imagePC.cameraDevice = UIImagePickerControllerCameraDeviceFront;
[self.imagePC release];
//判断后置摄像头是否可用
}else if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]){
NSLog(@"用后置摄像头");

self.imagePC = [[UIImagePickerController alloc]init];
self.imagePC.cameraDevice = UIImagePickerControllerCameraDeviceRear;
[self.imagePC release];
//两者都不行的话,从手机相册调取照片
}else{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"调用摄像头失败" message:@"请从手机相册中选取照片" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
[alertView release];
}

//初始化图片控制器对象
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
//sourceType资源样式
//设置图片选择器选择图片的样式
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//设置图片是否允许编辑
imagePicker.allowsEditing = YES;
//设置图片选择器代理对象为这个视图控制器
imagePicker.delegate = self;
//把相机推出来 模态
[self presentViewController:imagePicker animated:YES completion:nil];
//释放
[imagePicker release];

}

4.2从手机的图片库获取图片

- (void)pickerPictureFromPhotosAlbum{
//初始化图片控制器对象
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
//sourceType资源样式
//设置图片选择器选择图片的样式
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//设置图片是否允许编辑
imagePicker.allowsEditing = YES;
//设置图片选择器代理对象为这个视图控制器
imagePicker.delegate = self;
//把选择控制器推出来 模态
[self presentViewController:imagePicker animated:YES completion:nil];
//释放
[imagePicker release];
}

5.将选取好的照片保存到详情页的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//以相册作为字典,从中取出照片
self.aImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
//把选取框模态回去
[self dismissViewControllerAnimated:YES completion:nil];
}
测试效果:(由于mac端虚拟机无前后摄像头所以直接跳转相册选取)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: