欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-15 20:05:16
前言:相信大家都知道大部分的app都是有我的模块的,而在我的模块基本都有用户的头像等信息,并且是可以更改头像的。那么今天小编给大家简单介绍一下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。万事俱备,就差一个测试的苹果手机了,相机的测试需要使用真机测试。

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