iOS调用系统相册和相机

在实现更换头像的时候,我们很多时候都需要调用系统的相册或相机去进行操作,具体实现方法如下:

首先先在项目的info中进行设定,添加如下图红色箭头所指的两个设定:
请添加图片描述
然后在.h文件中

//定义两个属性,并签订两个协议
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

//用于显示图片的imageView
@property (nonatomic, strong) UIImageView *imageViewTest;
//调用显示相册相机的属性
@property (nonatomic, strong) UIImagePickerController *imagePicker;

@end

在.m文件中:
先实现一下imageViewTest函数,内容是对于imageViewTest属性的一些设定

- (UIImageView *) imageViewTest {
    if (!_imageViewTest) {
        _imageViewTest = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
        _imageViewTest.backgroundColor = [UIColor grayColor];
        _imageViewTest.center = self.view.center;
        //下方的firstImage.jpg为初始化图片,可自定义
        _imageViewTest.image = [UIImage imageNamed:@"firstImage.jpg"];
        //下方设置为YES使图片可以触发点击事件
        _imageViewTest.userInteractionEnabled = YES;
        UITapGestureRecognizer *clickTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseImage)];
        [_imageViewTest addGestureRecognizer:clickTap];
    }
    return _imageViewTest;
}

然后再实现刚才imageView的点击事件函数:
主要是添加一些对应的警示弹窗和点击后的执行代码

- (void) chooseImage {
    
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.delegate = self;
    self.imagePicker.allowsEditing = YES;
    
    UIAlertController *actionSheet = [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.imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
			
            [self presentViewController:self.imagePicker animated:YES completion:nil];
        }
    }];
  
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    	//使点击后跳转到系统的图库界面
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        self.imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
        
        [self presentViewController:self.imagePicker animated:YES completion:nil];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");
    }];
    
    [actionSheet addAction:cameraAction];
    [actionSheet addAction:photoAction];
    [actionSheet addAction:cancelAction];
    
    [self presentViewController:actionSheet animated:YES completion:nil];
    
}

最后实现一下获取选择的图片和返回的代码:

//获取选择的图片
//其中的info中含有所选中图片和其状态信息
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    //将所选图片设定为imageViewTest的显示内容
    self.imageViewTest.image = image;
}

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

完成的demo的代码如下:(.m文件中)

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:[self imageViewTest]];
}

- (UIImageView *) imageViewTest {
    if (!_imageViewTest) {
        _imageViewTest = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
        _imageViewTest.backgroundColor = [UIColor grayColor];
        _imageViewTest.center = self.view.center;
        //下方的firstImage.jpg为初始化图片,可自定义
        _imageViewTest.image = [UIImage imageNamed:@"firstImage.jpg"];
        _imageViewTest.userInteractionEnabled = YES;//这个一定要设置为YES,默认的为NO,NO的时候不可发生用户交互动作
        UITapGestureRecognizer *clickTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseImage)];
        [_imageViewTest addGestureRecognizer:clickTap];
    }
    return _imageViewTest;
}

- (void) chooseImage {
    
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.delegate = self;
    self.imagePicker.allowsEditing = YES;
    
    UIAlertController *actionSheet = [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.imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
            
            
            [self presentViewController:self.imagePicker animated:YES completion:nil];
        }
    }];
  
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        self.imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
        
        [self presentViewController:self.imagePicker animated:YES completion:nil];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");
    }];
    
    [actionSheet addAction:cameraAction];
    [actionSheet addAction:photoAction];
    [actionSheet addAction:cancelAction];
    
    [self presentViewController:actionSheet animated:YES completion:nil];
    
}

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

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

@end

此博客中内容学习自大佬博客:大佬博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值