【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];
}

实现效果:

相关推荐
敲代码的鱼3 小时前
PDF 预览与签名批注写回 支持安卓 iOS 鸿蒙 UTS插件
android·前端·ios
时光足迹5 小时前
uni-app 视频通话实战:康复师与患者视频问诊的 6 个致命 Bug 与解决方案
android·ios·uni-app
时光足迹6 小时前
JPush UniApp UTS 插件完全参考手册:API、事件与厂商通道一网打尽
vue.js·ios·uni-app
时光足迹6 小时前
极光推送全攻略(下):uni-app 代码实现与 iOS 排查实战
vue.js·ios·uni-app
时光足迹6 小时前
极光推送全攻略(上):被iOS证书折磨了三天,我写了一份前端也能看懂的避坑指南
前端·ios·uni-app
编程范式1 天前
SwiftUI 中图片如何适配可用空间
ios
counterxing2 天前
最近发现一个 Mac 工具,有点像把 Raycast、语音输入法、截图和录屏塞到了一起
macos·ai编程·claude
songgeb3 天前
启发式 UI 自动化:从线性剧本到每步读屏决策
ios·测试
壹方秘境7 天前
我用Go语言开发了一个跨平台的HTTPS抓包和调试工具
前端·后端·ios
元Y亨H10 天前
MacBook Air 开发神器:IDEA 与 PyCharm 极简安装及环境配置
macos