ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-11-27 21:58:14  阅读:187  来源: 互联网

标签:UIAlertAction 相册 NSLog iOS 相机 UIImagePickerController imagePicker


在大部分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(@"图片上传成功!");
    }];
}

效果:
请添加图片描述

标签:UIAlertAction,相册,NSLog,iOS,相机,UIImagePickerController,imagePicker
来源: https://blog.csdn.net/weixin_50990189/article/details/121583647

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有