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

实现效果:

相关推荐
watersink6 小时前
基于大模型的pc版语音对话问答
ide·macos·xcode
Alger_Hamlet6 小时前
OmniGraffle Pro for Mac思维导图
macos
Alger_Hamlet6 小时前
Ae After Effects2024 for Mac 视频处理
macos
Python之栈6 小时前
Python 3.13 正式支持 iOS:移动开发的新篇章
python·macos·objective-c·cocoa
白熊1887 小时前
Mac 本地化部署 dify
macos·dify
鸿蒙布道师8 小时前
鸿蒙NEXT开发Base64工具类(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
美狐美颜sdk9 小时前
美颜SDK兼容性挑战:如何让美颜滤镜API适配iOS与安卓?
android·深度学习·ios·美颜sdk·第三方美颜sdk·视频美颜sdk
Invisible_He10 小时前
iOS自定义collection view的page size(width/height)分页效果
ui·ios·swift·collection
小橙子207712 小时前
一条命令配置移动端(Android / iOS)自动化环境
android·ios·自动化
yidahis13 小时前
iOS启动优化 - 1分钟让你的启动速度降低 1s
ios·客户端