【iOS开发】—— 调用相机、相册

在大部分app中,都是会使用到调用相机和相册的,所以学习app开发就必须要学习一下调用相机和相册。

首先要先了解一下UIImagePickerController。

一、UIImagePickerController

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

UIImagePickerController–>UINavigationController–>UIViewController–>UIResponder–>NSObject

二、具体操作

调用相机时:

//访问相机操作
- (void)visitCamera {
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该app正在请求您的拍摄权限,是否同意?" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init];
        BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]; //判断相机可不可用
        if (!isCamera) {
            NSLog(@"没有摄像头");
            return;
        }
        imagePicker.delegate = self;   //设置代理
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; //数据来源于相机
        imagePicker.allowsEditing = YES;
            NSLog(@"=======确认使用相机========");
        }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"=======取消使用相机======");
        }];

    [alert addAction:okAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];

}

相机的类型:

typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
    UIImagePickerControllerCameraDeviceRear, //后置摄像头
    UIImagePickerControllerCameraDeviceFront //前置摄像头
} 

调用相册

//访问相册
- (void)visitPhoto {
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该app正在请求访问您的相册,是否同意访问?" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init];
        [self presentViewController:imagePicker animated:YES completion:nil];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //数据来源于相册
            NSLog(@"=======确认访问相册========");
        }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"=======取消访问相册======");
        }];

    [alert addAction:okAction];
    [alert addAction:cancelAction];
    
}

UIImagePickerController数据来源:

typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
    UIImagePickerControllerSourceTypePhotoLibrary, //来源于相册 
    UIImagePickerControllerSourceTypeCamera,  //相机
    UIImagePickerControllerSourceTypeSavedPhotosAlbum  //系统照片应用的照片,包含用设备拍摄的所有照片流
}

选取图片后的回调:

//访问相册后的回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
    NSLog(@"========所选图片信息=======\n");
    NSLog(@"%@", info);
    
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage]; //UIImagePickerControllerOriginalImage 图片可以编辑
    self.imageView.image = image;
    
    [picker dismissViewControllerAnimated:YES completion:^{
        NSLog(@"图片上传成功!");
    }];
}

效果:
【iOS开发】—— 调用相机、相册

上一篇:MySQL误操作恢复神器一家老小全在这里


下一篇:iOS工具 - NSLog日志打印出中文