在实现更换头像的时候,我们很多时候都需要调用系统的相册或相机去进行操作,具体实现方法如下:
首先先在项目的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
此博客中内容学习自大佬博客:大佬博客