iOS开发-调用系统相机和相册获取照片示例

 更新时间:2017年02月17日 10:18:07   作者:Abner_G  
这篇文章主要介绍了iOS开发-调用系统相机和相册获取照片示例的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

前言:相信大家都知道大部分的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。万事俱备,就差一个测试的苹果手机了,相机的测试需要使用真机测试。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • IOS实现的简单画板功能

    IOS实现的简单画板功能

    本文主要介绍了IOS实现简单画板的示例代码。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-04-04
  • 详解IOS判断当前网络状态的三种方法

    详解IOS判断当前网络状态的三种方法

    这篇文章主要介绍了详解IOS判断当前网络状态的三种方法,网络状态是非常重要的知识,感兴趣的同学,必须要看一下
    2021-04-04
  • iOS封装倒计时按钮HLCountDownButton示例详解

    iOS封装倒计时按钮HLCountDownButton示例详解

    这篇文章主要为大家介绍了iOS封装倒计时按钮HLCountDownButton示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • NSMutable 对象的坑解决分析

    NSMutable 对象的坑解决分析

    这篇文章主要为大家介绍了NSMutable 对象的坑解决分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • IOS实战之自定义转场动画详解

    IOS实战之自定义转场动画详解

    这篇文章主要介绍了IOS实战之自定义转场动画,CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • iOS编程学习中关于throttle的那些事

    iOS编程学习中关于throttle的那些事

    这篇文章主要给大家介绍了关于iOS编程学习中throttle的那些事,文中通过示例代码介绍的非常详细,对各位iOS的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-12-12
  • iOS实现多个垂直滑动条并列视图

    iOS实现多个垂直滑动条并列视图

    这篇文章主要为大家详细介绍了iOS实现多个垂直滑动条并列视图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • iOS App通信之local socket示例

    iOS App通信之local socket示例

    这篇文章主要介绍了iOS App之间的通信 -local socket示例的相关资料,需要的朋友可以参考下
    2016-09-09
  • iOS上下文实现评价星星示例代码

    iOS上下文实现评价星星示例代码

    这篇文章主要介绍了iOS上下文实现评价星星的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-03-03
  • Flutter Boost 混合开发框架

    Flutter Boost 混合开发框架

    Flutter是一个由C++实现的Flutter Engine和由Dart实现的Framework组成的跨平台技术框架,本文将在此做一个初步的讲解
    2021-08-08

最新评论