iOS中 讀取相冊(cè),調(diào)用系統(tǒng)相機(jī)

讀取相冊(cè)以及調(diào)取相機(jī),將圖片顯示到imageView上

布局:

1.創(chuàng)建imageView 和 button 并為button一個(gè)關(guān)聯(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.因?yàn)橛械膱?chǎng)景需要直接點(diǎn)擊圖片更換別的圖片,所以在imageView上添加輕拍動(dòng)作

- (void)addTapGestureOnImageView{

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pickerImage:)];

[self.aImageView addGestureRecognizer:tap];

[tap release];

}

3.實(shí)現(xiàn)輕拍動(dòng)作中方法

- (void)pickerImage:(UIButton *)button{

//添加ActionSheet控件,提示選項(xiàng)框,調(diào)出相機(jī)或拍攝圖片

//第一個(gè)參數(shù):是行為列表的標(biāo)題 一般為空

//第二個(gè)參數(shù):遵循代理

//第三個(gè)參數(shù):取消這個(gè)操作按鈕上 顯示的文字

//第四個(gè)參數(shù):destructive 破壞性的, 毀滅性的 自己理解吧 反正我寫的是拍照,執(zhí)行操作的意思

//第五個(gè)參數(shù):從相冊(cè)選取圖片

UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"從相冊(cè)選擇圖片", nil];

//在當(dāng)前界面顯示actionSheet對(duì)象

[actionSheet showInView:self.view];

[actionSheet release];

}

4.實(shí)現(xiàn)action代理協(xié)議中的方法

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

switch (buttonIndex) {

case 0:

//調(diào)用系統(tǒng)相機(jī),拍照

[self pickerPictureFromCamera];

break;

case 1:

[self pickerPictureFromPhotosAlbum];

default:

break;

}

}

4.1從攝像頭獲取圖片

- (void)pickerPictureFromCamera{

//判斷前攝像頭是否可用,如果不可用的話,用后攝像頭。如果后攝像頭也不可用的話用手機(jī)圖片庫(kù)

//判斷前置攝像頭是否可用

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];

//兩者都不行的話,從手機(jī)相冊(cè)調(diào)取照片

}else{

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"調(diào)用攝像頭失敗" message:@"請(qǐng)從手機(jī)相冊(cè)中選取照片" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];

[alertView show];

[alertView release];

}

//初始化圖片控制器對(duì)象

UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];

//sourceType資源樣式

//設(shè)置圖片選擇器選擇圖片的樣式

imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

//設(shè)置圖片是否允許編輯

imagePicker.allowsEditing = YES;

//設(shè)置圖片選擇器代理對(duì)象為這個(gè)視圖控制器

imagePicker.delegate = self;

//把相機(jī)推出來(lái) 模態(tài)

[self presentViewController:imagePicker animated:YES completion:nil];

//釋放

[imagePicker release];

}

4.2從手機(jī)的圖片庫(kù)獲取圖片

- (void)pickerPictureFromPhotosAlbum{

//初始化圖片控制器對(duì)象

UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];

//sourceType資源樣式

//設(shè)置圖片選擇器選擇圖片的樣式

imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

//設(shè)置圖片是否允許編輯

imagePicker.allowsEditing = YES;

//設(shè)置圖片選擇器代理對(duì)象為這個(gè)視圖控制器

imagePicker.delegate = self;

//把選擇控制器推出來(lái) 模態(tài)

[self presentViewController:imagePicker animated:YES completion:nil];

//釋放

[imagePicker release];

}

5.將選取好的照片保存到詳情頁(yè)的方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

//以相冊(cè)作為字典,從中取出照片

self.aImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];

//把選取框模態(tài)回去

[self dismissViewControllerAnimated:YES completion:nil];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容