讀取相冊以及調取相機,將圖片顯示到imageView上
布局:
1.創(chuàng)建imageView 和 button 并為button一個關聯(lián)pickerImage的事件
self.aImageView = [[UIImageView alloc]initWithFrame:CGRectMake(60, 100, 200, 200)];
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.實現(xiàn)輕拍動作中方法
- (void)pickerImage:(UIButton *)button{
//添加ActionSheet控件,提示選項框,調出相機或拍攝圖片
//第一個參數(shù):是行為列表的標題 一般為空
//第二個參數(shù):遵循代理
//第三個參數(shù):取消這個操作按鈕上 顯示的文字
//第四個參數(shù):destructive 破壞性的, 毀滅性的 自己理解吧 反正我寫的是拍照,執(zhí)行操作的意思
//第五個參數(shù):從相冊選取圖片
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"從相冊選擇圖片", nil];
//在當前界面顯示actionSheet對象
[actionSheet showInView:self.view];
[actionSheet release];
}
4.實現(xiàn)action代理協(xié)議中的方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
//調用系統(tǒng)相機,拍照
[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;
//把相機推出來 模態(tài)
[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;
//把選擇控制器推出來 模態(tài)
[self presentViewController:imagePicker animated:YES completion:nil];
//釋放
[imagePicker release];
}
5.將選取好的照片保存到詳情頁的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//以相冊作為字典附较,從中取出照片
self.aImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
//把選取框模態(tài)回去
[self dismissViewControllerAnimated:YES completion:nil];
}