最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS開發(fā)-調(diào)用系統(tǒng)相機(jī)和相冊獲取照片示例

 更新時(shí)間:2017年02月17日 10:18:07   作者:Abner_G  
這篇文章主要介紹了iOS開發(fā)-調(diào)用系統(tǒng)相機(jī)和相冊獲取照片示例的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

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

首先,需要遵循 UIImagePickerController 代理的兩個協(xié)議: <UIImagePickerControllerDelegate, UINavigationControllerDelegate>。為什么是兩個協(xié)議呢?你按著 command 鍵,點(diǎn)擊 UIImagePickerController 的 delegate 就會發(fā)現(xiàn)其實(shí)這個代理遵循了兩個協(xié)議。

#import "HeaderPhotoViewController.h"

@interface HeaderPhotoViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) UIImageView * imageView;
@end

@implementation HeaderPhotoViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.navigationItem.title = @"設(shè)置頭像";
  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 {

  //創(chuàng)建UIImagePickerController對象,并設(shè)置代理和可編輯
  UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
  imagePicker.editing = YES;
  imagePicker.delegate = self;
  imagePicker.allowsEditing = YES;

  //創(chuàng)建sheet提示框,提示選擇相機(jī)還是相冊
  UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"請選擇打開方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

  //相機(jī)選項(xiàng)
  UIAlertAction * camera = [UIAlertAction actionWithTitle:@"相機(jī)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    //選擇相機(jī)時(shí),設(shè)置UIImagePickerController對象相關(guān)屬性
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
    imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
    imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    //跳轉(zhuǎn)到UIImagePickerController控制器彈出相機(jī)
    [self presentViewController:imagePicker animated:YES completion:nil];
  }];

  //相冊選項(xiàng)
  UIAlertAction * photo = [UIAlertAction actionWithTitle:@"相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    //選擇相冊時(shí),設(shè)置UIImagePickerController對象相關(guān)屬性
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //跳轉(zhuǎn)到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的所有代碼都已經(jīng)給大家呈現(xiàn)出來了,最后一步就是配置plist文件,千萬不要忘了這個,要不會崩的。plist文件里邊添加調(diào)用相機(jī)的字段Privacy - Camera Usage Description 和調(diào)用相冊的字段:Privacy - Photo Library Usage Description。萬事俱備,就差一個測試的蘋果手機(jī)了,相機(jī)的測試需要使用真機(jī)測試。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • IOS實(shí)現(xiàn)的簡單畫板功能

    IOS實(shí)現(xiàn)的簡單畫板功能

    本文主要介紹了IOS實(shí)現(xiàn)簡單畫板的示例代碼。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-04-04
  • 詳解IOS判斷當(dāng)前網(wǎng)絡(luò)狀態(tài)的三種方法

    詳解IOS判斷當(dāng)前網(wǎng)絡(luò)狀態(tài)的三種方法

    這篇文章主要介紹了詳解IOS判斷當(dāng)前網(wǎng)絡(luò)狀態(tài)的三種方法,網(wǎng)絡(luò)狀態(tài)是非常重要的知識,感興趣的同學(xué),必須要看一下
    2021-04-04
  • iOS封裝倒計(jì)時(shí)按鈕HLCountDownButton示例詳解

    iOS封裝倒計(jì)時(shí)按鈕HLCountDownButton示例詳解

    這篇文章主要為大家介紹了iOS封裝倒計(jì)時(shí)按鈕HLCountDownButton示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • NSMutable?對象的坑解決分析

    NSMutable?對象的坑解決分析

    這篇文章主要為大家介紹了NSMutable?對象的坑解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • IOS實(shí)戰(zhàn)之自定義轉(zhuǎn)場動畫詳解

    IOS實(shí)戰(zhàn)之自定義轉(zhuǎn)場動畫詳解

    這篇文章主要介紹了IOS實(shí)戰(zhàn)之自定義轉(zhuǎn)場動畫,CAAnimation的子類,用于做轉(zhuǎn)場動畫,能夠?yàn)閷犹峁┮瞥銎聊缓鸵迫肫聊坏膭赢嬓Ч?,感興趣的小伙伴們可以參考一下
    2016-02-02
  • iOS編程學(xué)習(xí)中關(guān)于throttle的那些事

    iOS編程學(xué)習(xí)中關(guān)于throttle的那些事

    這篇文章主要給大家介紹了關(guān)于iOS編程學(xué)習(xí)中throttle的那些事,文中通過示例代碼介紹的非常詳細(xì),對各位iOS的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • iOS實(shí)現(xiàn)多個垂直滑動條并列視圖

    iOS實(shí)現(xiàn)多個垂直滑動條并列視圖

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)多個垂直滑動條并列視圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • iOS App通信之local socket示例

    iOS App通信之local socket示例

    這篇文章主要介紹了iOS App之間的通信 -local socket示例的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • iOS上下文實(shí)現(xiàn)評價(jià)星星示例代碼

    iOS上下文實(shí)現(xiàn)評價(jià)星星示例代碼

    這篇文章主要介紹了iOS上下文實(shí)現(xiàn)評價(jià)星星的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Flutter Boost 混合開發(fā)框架

    Flutter Boost 混合開發(fā)框架

    Flutter是一個由C++實(shí)現(xiàn)的Flutter Engine和由Dart實(shí)現(xiàn)的Framework組成的跨平臺技術(shù)框架,本文將在此做一個初步的講解
    2021-08-08

最新評論

澳门| 招远市| 泰宁县| 会昌县| 新河县| 岳池县| 搜索| 赤壁市| 墨江| 临颍县| 象州县| 渭源县| 安图县| 阜新市| 松原市| 宁河县| 和顺县| 乌恰县| 康保县| 都兰县| 永平县| 新化县| 武夷山市| 阿坝县| 沂水县| 秭归县| 新干县| 勃利县| 枣强县| 八宿县| 绥宁县| 海安县| 从江县| 九江县| 泾阳县| 冷水江市| 柳林县| 津市市| 左权县| 麻城市| 合水县|