国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS開發-調用系統相機和相冊獲取照片示例

2020-07-26 02:55:59
字體:
來源:轉載
供稿:網友

前言:相信大家都知道大部分的app都是有我的模塊的,而在我的模塊基本都有用戶的頭像等信息,并且是可以更改頭像的。那么今天小編給大家簡單介紹一下iOS開發中如何調用系統相機拍照或者相冊獲取照片。要獲取系統相機或者相冊,我們需要使用到 UIImagePickerController 這個類。下面我們來看一下如何實現:

首先,需要遵循 UIImagePickerController 代理的兩個協議: <UIImagePickerControllerDelegate, UINavigationControllerDelegate>。為什么是兩個協議呢?你按著 command 鍵,點擊 UIImagePickerController 的 delegate 就會發現其實這個代理遵循了兩個協議。

#import "HeaderPhotoViewController.h"@interface HeaderPhotoViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>@property (nonatomic, strong) UIImageView * imageView;@end@implementation HeaderPhotoViewController- (void)viewDidLoad {  [super viewDidLoad];  self.navigationItem.title = @"設置頭像";  self.view.backgroundColor = [UIColor whiteColor];  [self setNavigation];  [self addSubviews];  [self makeConstraintsForUI];}#pragma mark - set navigation- (void)setNavigation {  self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(selectPhoto:)];}#pragma mark - navitation item action- (void)selectPhoto:(UIBarButtonItem *)itemCamera {  //創建UIImagePickerController對象,并設置代理和可編輯  UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];  imagePicker.editing = YES;  imagePicker.delegate = self;  imagePicker.allowsEditing = YES;  //創建sheet提示框,提示選擇相機還是相冊  UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"請選擇打開方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];  //相機選項  UIAlertAction * camera = [UIAlertAction actionWithTitle:@"相機" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {    //選擇相機時,設置UIImagePickerController對象相關屬性    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;    imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;    imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];    imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;    //跳轉到UIImagePickerController控制器彈出相機    [self presentViewController:imagePicker animated:YES completion:nil];  }];  //相冊選項  UIAlertAction * photo = [UIAlertAction actionWithTitle:@"相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {    //選擇相冊時,設置UIImagePickerController對象相關屬性    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;    //跳轉到UIImagePickerController控制器彈出相冊    [self presentViewController:imagePicker animated:YES completion:nil];  }];  //取消按鈕  UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {    [self dismissViewControllerAnimated:YES completion:nil];  }];  //添加各個按鈕事件  [alert addAction:camera];  [alert addAction:photo];  [alert addAction:cancel];  //彈出sheet提示框  [self presentViewController:alert animated:YES completion:nil];}#pragma mark - add subviews- (void)addSubviews {  [self.view addSubview:self.imageView];}#pragma mark - make constraints- (void)makeConstraintsForUI {  __weak typeof(self)weakSelf = self;  [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {    make.size.mas_equalTo(CGSizeMake(Screen_Width, Screen_Width));    make.centerX.mas_equalTo(weakSelf.view.mas_centerX);    make.centerY.mas_equalTo(weakSelf.view.mas_centerY);  }];}#pragma mark - imagePickerController delegate- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {  [picker dismissViewControllerAnimated:YES completion:nil];  //獲取到的圖片  UIImage * image = [info valueForKey:UIImagePickerControllerEditedImage];  _imageView.image = image;}#pragma mark - setter and getter- (UIImageView *)imageView {  if (!_imageView) {    _imageView = [[UIImageView alloc] init];    _imageView.backgroundColor = [UIColor greenColor];    _imageView.contentMode = UIViewContentModeScaleAspectFill;  }  return _imageView;}@end

OK!demo的所有代碼都已經給大家呈現出來了,最后一步就是配置plist文件,千萬不要忘了這個,要不會崩的。plist文件里邊添加調用相機的字段Privacy - Camera Usage Description 和調用相冊的字段:Privacy - Photo Library Usage Description。萬事俱備,就差一個測試的蘋果手機了,相機的測試需要使用真機測試。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 梁山县| 永仁县| 虞城县| 连州市| 洪江市| 漳平市| 开鲁县| 札达县| 高碑店市| 庆阳市| 包头市| 沂南县| 石首市| 高州市| 德州市| 西青区| 囊谦县| 南城县| 锦州市| 周至县| 抚顺市| 内江市| 长岛县| 房山区| 福州市| 江北区| 马边| 惠水县| 临西县| 凌海市| 玉门市| 甘孜县| 江城| 疏附县| 中牟县| 垦利县| 兴安县| 文安县| 若尔盖县| 无锡市| 新晃|