objective-c xcode iOS调用系统相册和相机 - 下面是选取照片的过程
iOS调用系统相册和相机
前言实现步骤
前言
在写项目的时候,在更换头像时需要从相册中获取图片,这就涉及到调用系统的相机或相册来进行相关操作,因此我先提前学习一下。
这里需要用到UIImagePickerController 首先我们要在项目的info中添加这两个:
实现步骤
在使用前需要遵循两个协议
@interface ViewController : UIViewController
//用于显示图片的imageView
@property (nonatomic, strong) UIImageView *imageView;
//调用显示相册相机的属性
@property (nonatomic, strong) UIImagePickerController *imagePicker;
@end
创建一个imageView并给它添加点击事件
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(150, 300, 100, 100)];
_imageView.backgroundColor = [UIColor redColor];
_imageView.layer.masksToBounds = YES;
_imageView.layer.cornerRadius = 50;
//设置初始化图片
_imageView.image = [UIImage imageNamed:@"QQ20210826-0.JPG"];
//下方设置为YES使图片可以触发点击事件,必须设置
_imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *clickTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseImage)]; 拼多多帮助力入口
[_imageView addGestureRecognizer:clickTap];
[self.view addSubview:_imageView];
知识: UIImagePickerController 里有几个比较重要的属性. Source type: 这个参数是用来确定是调用摄像头还是调用图片库.
如果是 UIImagePickerControllerSourceTypeCamera 就是调用摄像头,如果是UIImagePickerControllerSourceTypePhotoLibrary 就是调用图片库如果是UIImagePickerControllerSourceTypeSavedPhotosAlbum 则调用iOS设备中的胶卷相机的图片. Media types:在拍照时,用来指定是拍静态的图片还是录像.kUTTypeImage 表示静态图片, kUTTypeMovie表示录像. Editing controls :用来指定是否可编辑.将allowsEditing 属性设置为YES表示可编辑,NO表示不可编辑.
实现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];
}
下面是选取照片的过程
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//将所选图片设定为imageViewTest的显示内容
self.imageView.image = image;
}
//从相机或相册界面弹出
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
推荐阅读