iOS中 讀取相簿,呼叫系統相機 技術分享
阿新 • • 發佈:2019-01-30
技術內容:分別讀取相簿以及調取相機,將圖片顯示到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端虛擬機器無前後攝像頭所以直接跳轉相簿選取)