【iOS-UIImagePickerController访问相机和相册】

【iOS-UIImagePickerController访问相机和相册】

  • [一. UIImagePickerController的介绍](#一. UIImagePickerController的介绍)
    • [1 . UIImagePickerController的作用](#1 . UIImagePickerController的作用)
    • [2 . UIImagePickerController的功能](#2 . UIImagePickerController的功能)
  • [二 . UIImagePickerController的测试程序](#二 . UIImagePickerController的测试程序)

一. UIImagePickerController的介绍

1 . UIImagePickerController的作用

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

2 . UIImagePickerController的功能

(1)访问照片库:使用sourceType属性设置为UIImagePickerControllerSourceTypePhotoLibrary,用户可以从设备的照片库中选择照片或视频。

(2)调用相机拍摄照片:使用sourceType属性设置为UIImagePickerControllerSourceTypeCamera,用户可以使用设备的摄像头拍摄照片。

(3)录制视频:使用sourceType属性设置为UIImagePickerControllerSourceTypeCamera,并将mediaTypes属性设置为支持视频录制的类型,用户可以使用设备的摄像头录制视频。

二 . UIImagePickerController的测试程序

中间的黄色区域是可以显示图片的区域,可以通过访问系统的相册的照片来改变该位置的图片。

上面这张图片就是通过访问相册改变的图片。

具体的代码步骤如下:

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

objectivec 复制代码
- (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;
}

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

objectivec 复制代码
- (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提供的三个代理方法之一。

objectivec 复制代码
// 取消选择,点击界面中的取消(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];
}

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

相关推荐
恋猫de小郭4 小时前
腾讯 Kuikly 正式开源,了解一下这个基于 Kotlin 的全平台框架
android·前端·ios
超人强6 小时前
一文搞定App启动流程、时间监测、优化措施
ios
一牛8 小时前
Appkit: 菜单是如何工作的
macos·ios·objective-c
tkevinjd9 小时前
文件操作、流对象示例
macos·objective-c·cocoa
JQShan11 小时前
React Native小课堂:箭头函数 vs 普通函数,为什么你的this总迷路?
javascript·react native·ios
画个大饼14 小时前
Swift与iOS内存管理机制深度剖析
开发语言·ios·swift
Ya-Jun20 小时前
常用第三方库:flutter_boost混合开发
android·flutter·ios
电鱼智能的电小鱼1 天前
基于 EFISH-SBC-RK3588 的无人机环境感知与数据采集方案
linux·网络·嵌入式硬件·数码相机·无人机·边缘计算
玫瑰花开一片一片1 天前
Flutter IOS 真机 Widget 错误。Widget 安装后系统中没有
flutter·ios·widget·ios widget