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

实现效果:

相关推荐
HarderCoder13 小时前
iOS 知识积累第一弹:从 struct 到 APP 生命周期的全景复盘
ios
goodSleep17 小时前
更新Mac OS Tahoe26用命令恢复 Mac 启动台时不小心禁用了聚焦搜索
macos
叽哥1 天前
Flutter Riverpod上手指南
android·flutter·ios
用户092 天前
SwiftUI Charts 函数绘图完全指南
ios·swiftui·swift
YungFan2 天前
iOS26适配指南之UIColor
ios·swift
权咚3 天前
阿权的开发经验小集
git·ios·xcode
用户093 天前
TipKit与CloudKit同步完全指南
ios·swift
小溪彼岸3 天前
macOS自带截图命令ScreenCapture
macos
法的空间3 天前
Flutter JsonToDart 支持 JsonSchema
android·flutter·ios
2501_915918413 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview