iOS——调用系统相册和相机

UIImagePickerController

调用相册需要用到UIImagePickerController,这是iOS系统提供的和系统的相册和相机交互的一个类,可以用来获取相册的照片,也可以调用系统的相机拍摄照片或者视频。该类的继承结构是:

UIImagePickerController-->UINavigationController-->UIViewController-->UIResponder-->NSObject

调用相册:

  • 首先需要导入对应的库和声明相关的属性:
objectivec 复制代码
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVKit/AVKit.h>


@interface ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImagePickerController *imagePicker;

@end
  • 获取相册信息:
objectivec 复制代码
//点击按钮执行调用手机相册的事件
- (void) selectPhoto {
    //检查当前设备是否支持使用相册作为照片的源
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        //创建UIImagePickerController实例对象
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        //设置照片的源为相册
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //设置允许用户对照片进行编辑、即用户可以对照片进行裁剪、旋转等操作
        picker.allowsEditing = YES;
        //设置picker的代理
        picker.delegate = self;
        //退出picker模态视图
        [self presentViewController:picker animated:YES completion:nil];
    } else {
        NSLog(@"照片源不可用");
    }
}
objectivec 复制代码
// UIImagePickerControllerDelegate的协议方法,用于在用户选择媒体(照片或视频)后进行回调,其中info是用户选择的媒体的信息的字典,字典中的键是枚举值UIImagePickerControllerInfoKey,它定义了一系列用于访问 info 字典中特定信息的键
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    //从info字典中获取编辑后的图片。UIImagePickerControllerEditedImage是一个键,表示用户在相册中选择照片并进行了编辑的情况下获取的照片,如果用户没有编辑,就使用UIImagePickerControllerOriginalImage获取原始图片
    UIImage *image = info[@"UIImagePickerControllerEditedImage"];
    [self.imageButton setImage:image forState:UIControlStateNormal];
    [self dismissViewControllerAnimated:YES completion:nil];
    //使用异步的方式将图片保存到沙盒的代码块
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //获取应用的沙盒目录路径数组
    //    NSSearchPathForDirectoriesInDomains: 这是一个 Foundation 框架提供的函数,用于获取指定域中指定文件夹的路径。在这里,我们使用它来获取 "Documents" 目录的路径。
    //    NSDocumentDirectory: 这是一个枚举值,表示我们想要获取的文件夹是应用程序沙盒中的 "Documents" 目录。"Documents" 目录是应用程序可以存储用户数据的地方。
    //    NSUserDomainMask: 这是一个枚举值,指定了我们要在哪个域中搜索路径。NSUserDomainMask 表示搜索用户的主目录。
    //    YES: 这是一个布尔值,表示是否展开波浪线 ("~") 缩写。设置为 YES 表示展开波浪线,得到的路径将是完整的绝对路径。
    //    NSArray *docs: 这是一个包含路径的数组,其中第一个元素就是 "Documents" 目录的路径。
        NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        //docs[0]就是"Documents" 目录的路径,然后将照片保存为A48538182112B436F433E06CD04E131A.jpg
        NSString *imagePath = [docs[0]stringByAppendingPathComponent:@"A48538182112B436F433E06CD04E131A.jpg"];
        //将图片转换为 PNG 格式的二进制数据
        NSData *imageData = UIImagePNGRepresentation(image);
        //将图片的二进制数据写入文件,实现保存文件到沙盒的操作。atomically:YES表示写入操作要么完全成功,要么完全不成功,确保文件的完整性
        [imageData writeToFile:imagePath atomically:YES];
    });
}

以下是一些 UIImagePickerControllerInfoKey 中定义的常见键:
UIImagePickerControllerMediaType : 表示媒体的类型,是图片还是视频。
UIImagePickerControllerOriginalImage : 表示原始图片。
UIImagePickerControllerEditedImage : 表示用户编辑后的图片。
UIImagePickerControllerMediaURL: 表示媒体的 URL,通常用于视频。

运行结果:


调用相机

objectivec 复制代码
UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init];
        BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]; //判断相机可不可用
        if (!isCamera) {
            NSLog(@"没有摄像头");
            return;
        }
        imagePicker.delegate = self;   //设置代理
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; //数据来源于相机
        imagePicker.allowsEditing = YES;
            NSLog(@"=======确认使用相机========");
        }];
  • 相机的类型:
objectivec 复制代码
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
    UIImagePickerControllerCameraDeviceRear, //后置摄像头
    UIImagePickerControllerCameraDeviceFront //前置摄像头
} 
相关推荐
测试员周周4 小时前
【Appium 系列】第14节-断言与验证 — Validator 的设计
android·人工智能·python·功能测试·ios·单元测试·appium
zandy10118 小时前
2026 主流技术栈:hermes agent多环境安装配置:Windows/Mac/Linux
linux·windows·macos
2501_916008898 小时前
Mac 上生成 AppStoreInfo.plist 文件,App Store 上架
android·macos·ios·小程序·uni-app·iphone·webview
人月神话-Lee10 小时前
【图像处理】高斯模糊——最优雅的模糊算法
图像处理·人工智能·算法·ios·ai编程·swift
@大迁世界10 小时前
iPhone 18e,可能不再“低一档”
ios·iphone
猫头虎10 小时前
【Trea】Trea国内版|国际版|海外版下载|Mac版|Windows版|Linux下载配置教程
linux·人工智能·windows·macos·aigc·ai编程·agi
大可ai中文版镜像10 小时前
OpenAI Codex Desktop App 保姆级安装教程(Windows / Mac)
人工智能·macos·codex
文滨10 小时前
10分钟搞定!Mac 配置 GitHub SSH 完全指南(小白也能看懂)
前端·macos·ssh·github
明月(Alioo)11 小时前
macOS 上 Charles 代理 HTTPS 抓包失败问题完整解决方案
网络协议·macos·https