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。万事俱备,就差一个测试的苹果手机了,相机的测试需要使用真机测试。

最后,希望能够帮助到有需要的猿友们,愿同是程序猿的我们能够共同学习进步,在开发的道路上越走越远!谢谢!

拓展:如果想要识别选择图片的二维码,需要在代理中进行修改,具体代码如下

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    //获取到的图片
    UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
    // 2、创建 CIImage
    CIImage *ciimage = [[CIImage alloc] initWithImage:image];
    // 3、识别精度
    NSDictionary *options = @{CIDetectorAccuracy: CIDetectorAccuracyHigh};
    // 4、创建识别器
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:options];
    
    NSArray *features = [detector featuresInImage:ciimage];
    
    NSString *message = nil;
    for (CIFeature *item in features) {
        if ([item isKindOfClass:[CIQRCodeFeature class]]) {
            message = [(CIQRCodeFeature *)item messageString];
            NSLog(@"message = %@", message);
            break;
        }
    }
}

设置是否能编辑图片

BOOL allowsEditing

设置当拍照完或在相册选完照片后,是否跳到编辑模式进行图片剪裁即是否允许编辑

showsCameraControls=Yes时才有效果。

[imagePicker setAllowsEditing:YES]; 

取消编辑的话要将其置为NO,同时获取到的图片类型要修改为UIImagePickerControllerOriginalImage

[imagePicker setAllowsEditing:NO]; 

扫描二维码关注公众号,回复: 16049913 查看本文章

UIImage * image = [info valueForKey:UIImagePickerControllerOriginalImage];

参考:iOS开发-调用系统相机和相册获取照片 - 简书ios基础篇(一)—— UIImagePickerController类_一颗程序媛0915的博客-CSDN博客

猜你喜欢

转载自blog.csdn.net/qq_43441647/article/details/130504701