UIImagePickerController和UIAlertController結合使用,uiimagepicker多選
在處理設定檔 - 頭像的時候,通常有兩個選項,一個是調用系統相機,一個是調用系統相簿。這裡要使用的就是UIImagePickerController方法。
在頭像位置的imageView添加一個手勢,或者添加一個透明的按鈕,用來實現click方法
- (IBAction)click:(id)sender{
//建立提醒視圖
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提醒" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"相機" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
//判斷裝置是否存在網路攝影機,有就調用系統相機,沒有,就提醒使用者
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//建立相機
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//檔案由來
picker.sourceType = UIImagePickerControllerSourceTypeCamera; //指定資料來源來自於相機
picker.delegate = self;// 指定代理
picker.allowsEditing = YES; //允許編輯
//模態彈出
[self presentViewController:picker animated:YES completion:nil];
}else{
//沒有網路攝影機,提醒使用者 您的裝置沒有網路攝影機
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"您的裝置沒有網路攝影機" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:nil];
[alertController addAction:alertAction1];
[self presentViewController:alertController animated:YES completion:nil];
}
}];
[alertController addAction:alertAction];
UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"相簿" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
UIImagePickerController *pickerC = [[UIImagePickerController alloc] init];
pickerC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//指定資料來源為相簿
pickerC.delegate = self; //指定代理
pickerC.allowsEditing = YES; // 允許編輯
[self presentViewController:pickerC animated:YES completion:nil];
}];
[alertController addAction:alertAction2];
[self presentViewController:alertController animated:YES completion:nil];
}
//選取圖片之後執行的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(@"%@",info);
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
self.photoImage.image = image;
[picker dismissViewControllerAnimated:YES completion:nil];
}