【iOS】UIImagePickerController

【iOS】UIImagePickerController

前言

笔者简单学习了iOS开发如何调用本地的一个相册的内容,下面简单介绍一下相关内容。

介绍

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

下面笔者直接通过一个例子来介绍对应的一个内容

  • viewDidLoad部分
objc 复制代码
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *pickImageButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [pickImageButton setTitle:@"选择图片" forState:UIControlStateNormal]; //设置了一个button
    [pickImageButton addTarget:self action:@selector(pickImage) forControlEvents:UIControlEventTouchUpInside]; // 通过点击时间打开对应的图片
    pickImageButton.frame = CGRectMake(100, 100, 150, 40);
    [self.view addSubview:pickImageButton];

}
  • 访问相册内容
objc 复制代码
- (void)pickImage {
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;  // 设置代理
    
    // 选择图片来源:相册
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    // 展示 UIImagePickerController
    [self presentViewController:imagePickerController animated:YES completion:nil];
}
  • 选择图片
objc 复制代码
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info { //后面的这个参数是有关于字典的内容,这个字典是用来访问图片的
    // 获取选择的图片
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    
    
    // 例如显示在 ImageView 上
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(50, 200, 200, 200);
    [self.view addSubview:imageView];
    
    // 关闭 UIImagePickerController
    [picker dismissViewControllerAnimated:YES completion:nil];
}

实现效果:

相关推荐
Dachui_11221 小时前
MacOS上如何运行内网穿透详细教程
macos
玫瑰花开一片一片2 小时前
Flutter IOS 真机 Widget 错误。Widget 安装后系统中没有
flutter·ios·widget·ios widget
Bruce_Liuxiaowei3 小时前
使用Python脚本在Mac上彻底清除Chrome浏览历史:开发实战与隐私保护指南
chrome·python·macos
烎就是我4 小时前
100行代码swift从零实现一个iOS日历
ios·swift
iFlyCai6 小时前
对Mac文字双击或三击鼠标左键没有任何反应
macos·计算机外设
鸿蒙布道师19 小时前
鸿蒙NEXT开发通知工具类(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
鸿蒙布道师19 小时前
鸿蒙NEXT开发网络相关工具类(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
請你喝杯Java19 小时前
Mac上Cursor无法安装插件解决方法
macos·cursor·vsode
网络之路Blog1 天前
【实战中提升自己】内网安全部署之端口隔离与MAC地址认证
安全·macos·网络之路一天·华为华三数通基础·华为华三企业实战架构·华为中小型企业实战·华为华三计算机网络基础