iOS——调用系统相册和相机

iOS——调用系统相册和相机

背景

我们在许多app里可以发现在换头像的时候一般有两个选择,一个是选择本地照片,一个选择是相机也就是直接拍一张照片当头像,另外还有就是可以去修改照片尺寸,所以我们就要知道这些功能是怎么实现的。

实现方法

首先我们在调用系统相册或者是调用照相机的时候需要用到UIImagePickerController,使用UIImagePickerController 需要有两个代理,分别是:UIImagePickerControllerDelegate,
UINavigationControllerDelegate。

UIImagePickerControllerSourceType //用来设置调用UIImagePickerController的类型
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;//是调用相机。

imagePickerController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;// 是调用系统相册。

 imagePickerController.allowsEditing=YES;//表示可以编辑对照片进行编辑,会多出来一个正方形的框。

首先我们需要在项目的info中添加:
在这里插入图片描述

Privacy-Camera Usage Description//App使用相机的权限
Privacy - Photo Library Usage Description//App使用系统相册的权限

我们先在.h文件中添加协议,以及成员变量:

@interface ViewController : UIViewController<UIImagePickerControllerDelegate,
UINavigationControllerDelegate>

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImagePickerController *imagePicker;

@end

然后在.m文件中先将iamgeView初始化:

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
        _imageView.center = self.view.center;
        _imageView.image = [UIImage imageNamed:@"bgImage.jpg"];
        _imageView.userInteractionEnabled = YES;//默认为NO,一定要手动设置成YES,这样才可以发生用户交互工作
        UITapGestureRecognizer *clickTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseImage)];//设置手势识别器,并且添加点击事件
        [_imageView addGestureRecognizer:clickTap];
        
    }
    return _imageView;
}

然后我们将对应的点击事件函数进行实现:

- (void)chooseImage {
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.delegate = self;
    self.imagePicker.allowsEditing = YES;
    
    UIAlertController *action = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
                       self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                       [self presentViewController:self.imagePicker animated:YES completion:nil];
                   }
            }];
    
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"从本地相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                [self presentViewController:self.imagePicker animated:YES completion:nil];
            }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                NSLog(@"点击了取消");
            }];
            
    [action addAction:cameraAction];
    [action addAction:photoAction];
    [action addAction:cancelAction];
    [self presentViewController:action animated:YES completion:nil];
}

主要就是添加了一个提示框,根据前边定义的手势识别器去触发函数,达到人机交互功能。

//获取选择的图片
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.imageView.image = image;
}
    

//从相机或者相册界面弹出
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

还有另一个方法,可以看看这位大佬:
iOS调用系统相机,访问系统相册功能实现

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值