[iOS开发]iOS调用系统相册相机

Posted Billy Miracle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[iOS开发]iOS调用系统相册相机相关的知识,希望对你有一定的参考价值。

引入框架:

#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>

实现代理:

UIImagePickerControllerDelegate

建一个imageView,并为它添加一个点击事件

	_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    _imageView.backgroundColor = [UIColor grayColor];
    _imageView.userInteractionEnabled = YES;
    [self.view addSubview: _imageView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singerTap:)];
    [_imageView addGestureRecognizer:tap];

点击事件响应函数:

- (void)singerTap:(UITapGestureRecognizer *)gesture 
    
    //UIAlertControllerStyleActionSheet弹窗在屏幕下面
    UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"选取图片" message:nil preferredStyle: UIAlertControllerStyleActionSheet];
    UIAlertAction *cameraAction=[UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) 
        //点语法
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        self->_imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        self->_imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen;
        
        //录制视频时长,默认10s
        self->_imagePickerController.videoMaximumDuration = 15;
        //相机类型(拍照、录像...)字符串需要做相应的类型转换
        self->_imagePickerController.mediaTypes = @[(NSString *)kUTTypeMovie,(NSString *)kUTTypeImage];
        //视频上传质量
        //UIImagePickerControllerQualityTypeHigh高清
        //UIImagePickerControllerQualityTypeMedium中等质量
        //UIImagePickerControllerQualityTypeLow低质量
        //UIImagePickerControllerQualityType640x480
        self->_imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
        //设置摄像头模式(拍照,录制视频)为录像模式
        self->_imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
        
        [self presentViewController:self->_imagePickerController animated:YES completion:nil];
    ];
    
    UIAlertAction *photosAlbumAction=[UIAlertAction actionWithTitle:@"图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) 
        //点语法
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        self->_imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:self->_imagePickerController animated:YES completion:nil];
    ];
    
    UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) 
        
        [self dismissViewControllerAnimated:YES completion:nil];
    ];
    //判断是否支持相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
        [alert addAction:cameraAction];
    
    [alert addAction:photosAlbumAction];
    [alert addAction:cancelAction];
    
    [self presentViewController:alert animated:YES completion:nil];

选取后回调函数:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info 
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    //原始照片
//    _imageView.image = [info objectForKey: UIImagePickerControllerOriginalImage];
    //编辑后照片
    _imageView.image = [info objectForKey: UIImagePickerControllerEditedImage];

以上是关于[iOS开发]iOS调用系统相册相机的主要内容,如果未能解决你的问题,请参考以下文章

iOS----------调用系统照相机和相册

NR - iOS / Android 相机/相册/日历/定位 等权限 检测+申请代码

NR - iOS / Android 相机/相册/日历/定位 等权限 检测+申请代码

NR - iOS / Android 相机/相册/日历/定位 等权限 检测+申请代码

iOS 使用系统相机相册显示中文

iOS10相册相机闪退bug-b