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

实现效果:

相关推荐
PJHubs17 小时前
把一年前写给自己用的翻译软件开源了 | TranslateP
ios·产品
Antonio91519 小时前
【Swift】UIKit:UIAlertController、UIImageView、UIDatePicker、UIPickerView和UISwitch
ios·cocoa·swift
2501_9417987320 小时前
C++高性能音频处理与实时特征提取实战分享:多线程信号处理与低延迟优化经验
ide·macos·xcode
星卯教育tony1 天前
Typora下载与Windows+Mac环境下使用教程
macos
laocaibulao1 天前
mac电脑brew安装libnghttp3.rb失败咋办
macos
wotaifuzao1 天前
(七)深入探讨BLE MAC 地址的隐私博弈:技术与隐私的较量
经验分享·物联网·macos·蓝牙·射频工程·ble
fruge1 天前
移动端 H5 兼容问题合集:iOS 与 Android 的差异化处理
android·ios
2501_915909061 天前
iOS 上架需要什么东西?一次从准备清单到实操流程的完整技术拆解
android·macos·ios·小程序·uni-app·cocoa·iphone
胖虎11 天前
iOS 如何自定义第一个显示的视图(含 SceneDelegate 删除指南)
ios·ios第一个页面·ios设置首页·scenedelegate
QuantumLeap丶1 天前
《Flutter全栈开发实战指南:从零到高级》- 17 -核心动画
android·flutter·ios