温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS开发如何实现调用系统相机和相册获取照片功能

发布时间:2021-07-07 13:48:49 来源:亿速云 阅读:595 作者:小新 栏目:移动开发

小编给大家分享一下iOS开发如何实现调用系统相机和相册获取照片功能,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

首先,需要遵循 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开发如何实现调用系统相机和相册获取照片功能”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios
AI