iOS--UIimagePickerController
- 前记
- UiimagePickerControlller
- viewdidload
- presstap
- gettoCamera
- gettoPhoto
- 切换头像
- UIimagePickerController常用代理方法
- 演示图片
前记
写完知乎日报之后,不知道该学些什么东西,但总的和以后写的项目有关,所以看学长的博客先学习一项相机和相册的调用
UiimagePickerControlller
这是iOS中自带得一个UI类,可以实现对相机及相册的调用和对媒体的处理。
UIImagePickerController 是 iOS 开发中的一个内置视图控制器,用于方便地实现图像和视频的选择和拍摄功能。它提供了一个用户界面,允许用户从设备的相册库中选择照片或视频,或者使用设备的摄像头进行拍摄。
使用 UIImagePickerController 可以实现以下功能:
从相册中选择照片:用户可以浏览设备相册中的照片,并选择其中的一张照片作为所选项。
从相册中选择视频:用户可以浏览设备相册中的视频,并选择其中的一个视频作为所选项。
拍摄照片:用户可以使用设备的摄像头拍摄照片,并将其作为所选项。
拍摄视频:用户可以使用设备的摄像头录制视频,并将其作为所选项。
提供图像编辑功能:UIImagePickerController 还提供了一些简单的图像编辑功能,如裁剪和调整大小等。
我访问他的定义了解到它是UINavigationController的子类,也就是说它是一个Controller,所以基本的操作就是presentviewcontroller ;至于功能是调用相机还是相册由属性决定 ;
viewdidload
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.headimageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"biaoqing.png"] ];
self.headimageview.userInteractionEnabled = YES ;
UITapGestureRecognizer* tapgesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(presstap)] ;
self.headimageview.frame = CGRectMake(100, 100, 100, 100) ;
[self.headimageview addGestureRecognizer:tapgesture] ;//通过手势来添加imageview的点击函数
[self.view addSubview:self.headimageview] ;
}
presstap
- (void)presstap {
UIAlertController* alertion = [UIAlertController alertControllerWithTitle:@"please" message:@"select" preferredStyle:UIAlertControllerStyleActionSheet] ;
UIAlertAction* camera = [UIAlertAction actionWithTitle:@"camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self gettoCamera] ;
}] ;
UIAlertAction* photo = [UIAlertAction actionWithTitle:@"photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self gettoPhoto] ;
}] ;
UIAlertAction* cancal = [UIAlertAction actionWithTitle:@"cancal" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}] ;
[alertion addAction:camera] ;
[alertion addAction:photo] ;
[alertion addAction:cancal] ;
[self presentViewController:alertion animated:YES completion:nil] ;
}
gettoCamera
- (void)gettoCamera {
//初始化Controller
self.imagepickercontroller = [[UIImagePickerController alloc] init] ;
//设置代理
self.imagepickercontroller.delegate = self ;
//设置sourceType来决定访问相机还是相册
self.imagepickercontroller.sourceType = UIImagePickerControllerSourceTypeCamera ;
//设置改Controller为全屏
self.imagepickercontroller.modalPresentationStyle = UIModalPresentationFullScreen ;
//self.imagepickercontroller.cameraCaptureMode 是 UIImagePickerController 的一个属性,用于设置相机的捕捉模式。在你提供的代码中,设置为 UIImagePickerControllerCameraCaptureModePhoto 表示将相机设置为仅拍摄照片的模式。
self.imagepickercontroller.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto ;
//跳转
[self presentViewController:self.imagepickercontroller animated:YES completion:nil] ;
}
gettoPhoto
- (void)gettoPhoto {
//初始化Controller
self.imagepickercontroller = [[UIImagePickerController alloc] init] ;
//设置代理
self.imagepickercontroller.delegate = self ;
//设置相册照片允许选择
self.imagepickercontroller.allowsEditing = YES ;
//设置sourceType访问相册
self.imagepickercontroller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary ;
[self presentViewController:self.imagepickercontroller animated:YES completion:nil] ;
}
切换头像
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
//切换头像
[self.imagepickercontroller dismissViewControllerAnimated:YES completion:nil] ;
UIImage* image = [info valueForKey:UIImagePickerControllerEditedImage] ;
self.headimageview.image = image ;
}
注意一下,最后一个方法是代理方法,要实现代理才能使用,
@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, strong) UIImagePickerController* imagepickercontroller ;
@property (nonatomic, strong) UIImageView* headimageview ;
@end
UIimagePickerController常用代理方法
1.imagePickerController(_:didFinishPickingMediaWithInfo:):
当用户选择或拍摄媒体后,该方法会被调用。你可以在该方法中获取选择的图像或视频,并对其进行处理。
2.imagePickerControllerDidCancel(_):
当用户取消选择或拍摄媒体时,该方法会被调用。你可以在该方法中执行相应的取消操作。
演示图片