一. UIImagePickerController的介绍

1 . UIImagePickerController的作用

UIImagePickerController是iOS平台上的一个类,用于在应用程序中访问设备的照片库、相机和视频录制功能。它提供了一个用户界面,使用户可以设备媒体库选择照片或视频,或者使用设备的摄像头拍摄照片或录制视频。在这里我们先只介绍一下访问相机和相册两个功能

2 . UIImagePickerController的功能

(1)访问照片库:使用sourceType属性设置为UIImagePickerControllerSourceTypePhotoLibrary用户可以设备的照片库中选择照片或视频
(2)调用相机拍摄照片:使用sourceType属性设置为UIImagePickerControllerSourceTypeCamera,用户可以使用设备的摄像头拍摄照片。
(3)录制视频:使用sourceType属性设置为UIImagePickerControllerSourceTypeCamera,并将mediaTypes属性设置支持视频录制的类型用户可以使用设备的摄像头录制视频。

二 . UIImagePickerController的测试程序

在这里插入图片描述
中间的黄色区域可以显示图片区域,可以通过访问系统相册的照片来改变该位置图片
在这里插入图片描述
上面这张图片就是通过访问相册改变的图片

具体的代码步骤如下

首先,往视图控制器上面添加导航栏,往导航栏上面添加一张图,并且给图片加上点击事件,并且添加一视图区域(黄色区域),通过点击事件来选择怎样改变黄色区域视图

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =  UIColor.whiteColor;
    // Do any additional setup after loading the view.
    
    //添加导航栏的方法调用
    [self addNavigation];
    
    //添加视图区域
    self.headImageview = [[UIImageView alloc] initWithFrame:CGRectMake(120, 200, 153, 153)];
    self.headImageview.backgroundColor = UIColor.yellowColor;
    [self.view addSubview:self.headImageview];
}

- (void) addNavigation {
    //手动设置导航
    UINavigationBarAppearance* apperance = [UINavigationBarAppearance new];
    [apperance configureWithOpaqueBackground];
    apperance.backgroundColor = [UIColor colorWithRed:55/255.0 green: 130/255.0 blue:223/255.0 alpha:1.0];
    apperance.shadowColor = [UIColor clearColor];
    self.navigationController.navigationBar.standardAppearance = apperance;
    self.navigationController.navigationBar.scrollEdgeAppearance = self.navigationController.navigationBar.standardAppearance;

    //设置一个自定义视图添加导航栏上。
    UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 395, 44)];
    view1.center = self.navigationController.navigationBar.center;

    self.avatar = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xiangji.png"]];
    self.avatar.frame = CGRectMake(310, -5, 44, 44);
    self.avatar.userInteractionEnabled = YES;
    [view1 addSubview: self.avatar];

	//添加点击事件
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(catchImage)];
    [self.avatar addGestureRecognizer:tap];

    //将 view1 设置当前视图控制器的导航标题视图
    self.navigationItem.titleView = view1;
}

然后点击事件中实现是想要通过相机还是相册来改变视图。

- (void) catchImage {
    self.imagePickController = [[UIImagePickerController alloc] init];
    self.imagePickController.delegate = self;
    self.imagePickController.allowsEditing = YES;
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction* camera = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self toCamera];
    }];
    
    UIAlertAction* album = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self toAlbum];
    }];
    
    UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }];
    
    [alert addAction: camera];
    [alert addAction: album];
    [alert addAction: cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void) toCamera {
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
       self.imagePickController.sourceType = UIImagePickerControllerSourceTypeCamera;
       self.imagePickController.modalPresentationStyle = UIModalPresentationFullScreen;
       [self presentViewController:self.imagePickController animated:YES completion:nil];
    } else {
        NSLog(@"无法打开相机");
    }
    
}

- (void) toAlbum  {
    self.imagePickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    self.imagePickController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:self.imagePickController animated:YES completion:nil];
}

但是要注意的是,模拟机是无法打开相机的,所以就会出现== Thread 1: “Source type 1 not available”==,然后程序就会崩溃

最后就是通过一些协议方法来实现其他的功能。
imagePickerController:(UIImagePickerController *)picker是UIImagePickerController提供的三个代理方法之一。

// 取消选择,点击界面中的取消(Cancel)按钮触发
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
 
}
 
// 选择完成,点击界面中的某个图片或者选择(Choose)按钮触发
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.headImageview.image = image;
    [picker dismissViewControllerAnimated:YES completion:nil];
}

后续还会根据项目需要学习心新的知识

原文地址:https://blog.csdn.net/qq_72437394/article/details/134632863

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_16771.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注