IOS開啟照相機與本地相簿選擇圖片
阿新 • • 發佈:2019-01-31
最近正好專案裡面要整合“開啟照相機與本地相簿選擇圖片”的功能,今天就在這邊給大家寫一個演示程式;開啟相機拍攝後或者在相簿中選擇一張照片,然後將它顯示在介面上。好了廢話不多說,因為比較簡單直接上原始碼。
首先,我們在標頭檔案中新增需要用到的actionSheet控制元件,顯示圖片的UIImageView控制元件,並且加上所需要的協議
#import <UIKit/UIKit.h> @interface ImagePickerViewController : UIViewController<UIImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate> @property (strong, nonatomic) IBOutlet UIImageView *headImage; @property (strong, nonatomic) UIActionSheet *actionSheet; - (IBAction)clickPickImage:(id)sender; @end
通過點選我設定在介面中的按鈕來撥出actionSheet控制元件,來選擇相應的操作拍照或是在相簿中選擇相片,程式碼如下:
// // ImagePickerViewController.m // testAuto // // Created by silicon on 15/5/9. // Copyright (c) 2015年 silicon. All rights reserved. // #import "ImagePickerViewController.h" @interface ImagePickerViewController () @end @implementation ImagePickerViewController @synthesize actionSheet = _actionSheet; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /** @ 呼叫ActionSheet */ - (void)callActionSheetFunc{ if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"選擇影象" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"從相簿選擇", nil]; }else{ self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"選擇影象" delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"從相簿選擇", nil]; } self.actionSheet.tag = 1000; [self.actionSheet showInView:self.view]; } // Called when a button is clicked. The view will be automatically dismissed after this call returns - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ if (actionSheet.tag == 1000) { NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // 判斷是否支援相機 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { switch (buttonIndex) { case 0: //來源:相機 sourceType = UIImagePickerControllerSourceTypeCamera; break; case 1: //來源:相簿 sourceType = UIImagePickerControllerSourceTypePhotoLibrary; break; case 2: return; } } else { if (buttonIndex == 2) { return; } else { sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; } } // 跳轉到相機或相簿頁面 UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; imagePickerController.sourceType = sourceType; [self presentViewController:imagePickerController animated:YES completion:^{ }]; } } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissViewControllerAnimated:YES completion:^{ }]; UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; self.headImage.image = image; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ - (IBAction)clickPickImage:(id)sender { [self callActionSheetFunc]; } @end
程式碼比較簡單,也容易理解,執行的效果如下: