iOS基础

UIImagePickerController拍照录像相册

2018-04-23  本文已影响5人  MrCoderLin

UIImagePickerController

iOS系统相机、相册功能全部依托于图像选取控制器UIImagePickerController,在使用该控制器时,我们需要按照如下步骤进行

  1. 检查指定的资源类型是否可用
  2. 检查指定资源类型下是否支持指定的媒体类型
  3. 检查用户对相机、相册的授权状态
  4. 初始化并弹出图像选取控制器
  5. 处理操作完成后的代理回调

使用注意:
在使用系统相机、相册时,发现系统语言都是英文,比如"取消"显示为"Cancel",如何才能调整为中文
在info.plist文件中有一个Localized resources can be mixed,默认为NO,修改为YES即可,这样会随着系统语言变化

代码:

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

// 调用相机录像功能
- (void)takeMovie {
    /**
     * UIImagePickerControllerSourceTypeCamera // 相机类型
     * UIImagePickerControllerSourceTypePhotoLibrary // 照片库类型,相当于系统应用"照片"中的"相簿/Photos"栏加上"照片/Moments"
     * UIImagePickerControllerSourceTypeSavedPhotosAlbum // 照片类型,相当于系统应用"照片"中的"照片/Moments"栏
     */
    // 检查指定的资源类型相机是否可用
    if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        /**
         * AVAuthorizationStatusNotDetermined // 用户没有选择是否授权使用
         * AVAuthorizationStatusRestricted // 用户禁止使用,且授权状态不可修改,可能由于家长控制功能
         * AVAuthorizationStatusDenied // 用户已经禁止使用
         * AVAuthorizationStatusAuthorized // 用户已经授权使用
         */
        //判断是否拥有拍摄权限
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {
            // 用户拒绝录像
            NSLog(@"用户拒绝录像");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"相机权限未开启,请进入系统【设置】>【隐私】>【相机】中打开开关,开启相机功能" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
            [alert show];
            return;
        }
    }
    // 录像
    self.imagePicker.mediaTypes  =  @[(NSString *)kUTTypeMovie];
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:self.imagePicker animated:YES completion:nil];
}

// 调用相机
- (void)takePhotoClick {
    //  检查指定的资源类型相机是否可用
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        // 检查用户对相机的授权状态
        AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied) {
            // 用户拒绝访问相册
            NSLog(@"用户拒绝访问相机");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"相机权限未开启,请进入系统【设置】>【隐私】>【相机】中打开开关,开启相机功能" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
            [alert show];
            return;
        }
    } else {
        // 不支持相机
        NSLog(@"不支持相机");
    }
    // 相机
    self.imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:self.imagePicker animated:YES completion:nil];
}
// 调用相册
- (void)albumClick {
    // 检查指定的资源类型相册是否可用
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
        // 检查用户对相册授权状态 ALAuthorizationStatus deprecated in iOS9
        ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
        if (status == ALAuthorizationStatusDenied || status == ALAuthorizationStatusRestricted) {
            // 用户拒绝访问相册
            NSLog(@"用户拒绝访问相册");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"相机权限未开启,请进入系统【设置】>【隐私】>【照片】中打开开关,开启相机功能" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
            [alert show];
            return ;
        }
        //    PHAuthorizationStatus statu = [PHPhotoLibrary authorizationStatus];
        //    if (statu == PHAuthorizationStatusDenied || statu == PHAuthorizationStatusRestricted) {
        
        //    }
        
    } else {
        NSLog(@"不支持相册");
    }
    // 相册
    // 只能选取图片 要获取视频 添加 kUTTypeMovie
    self.imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self presentViewController:self.imagePicker animated:YES completion:nil];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}
/**
 * UIImagePickerControllerMediaType // 媒体类型(kUTTypeImage、kUTTypeMovie等)
 * UIImagePickerControllerOriginalImage // 原始图片
 * UIImagePickerControllerEditedImage // 编辑后图片
 * UIImagePickerControllerCropRect // 裁剪尺寸
 * UIImagePickerControllerMediaMetadata // 拍照的元数据
 * UIImagePickerControllerMediaURL // 媒体的URL
 * UIImagePickerControllerReferenceURL // 引用相册的URL
 * UIImagePickerControllerLivePhoto // PHLivePhoto
 */
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    NSLog(@"%@",info);
    
    NSString *mediaType = info[UIImagePickerControllerMediaType];
    if (mediaType == (NSString *)kUTTypeMovie) {
        // 视频 UIImagePickerControllerMediaURL
        NSString *mediaURL = info[UIImagePickerControllerMediaURL];
        NSLog(@"%@",mediaURL);
    } else if (mediaType == (NSString *)kUTTypeImage) {
        // 图片 UIImagePickerControllerOriginalImage UIImagePickerControllerEditedImage
        NSLog(@"%@",info[UIImagePickerControllerOriginalImage]);
    }
//    kUTTypeVideo
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    //跳入当前App设置界面,
    // deprecated iOS10
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    // iOS 10之后才能用
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
}

#pragma mark - Lazy Loading
- (UIImagePickerController *)imagePicker {
    if (_imagePicker == nil) {
        _imagePicker = [[UIImagePickerController alloc] init];
        _imagePicker.delegate = self;
        
        // 时长 录制质量
        _imagePicker.videoMaximumDuration = 10;
        _imagePicker.videoQuality = UIImagePickerControllerQualityTypeHigh;
        
        // 可编辑
        _imagePicker.allowsEditing = YES;
        
        /** 相机 相册 录像不同功能更改sourceType mediaTypes */
        // 相机功能 照片库UIImagePickerControllerSourceTypePhotoLibrary UIImagePickerControllerSourceTypeSavedPhotosAlbum
        _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        /**
         * kUTTypeMovie 视频类型
         * kUTTypeImage 图片类型
         */
        // 媒体类型 录制视频  相机/相册 kUTTypeImage
        _imagePicker.mediaTypes = @[(NSString *)kUTTypeMovie];
        /**
        // 设置照片选取器的导航栏颜色 字体
        UINavigationBar *naviBar = _imagePicker.navigationBar;
        [naviBar setBarTintColor:[UIColor redColor]];
        naviBar.tintColor = [UIColor whiteColor];
        // 设置不透明
        [naviBar setTranslucent:NO];
        // 更改titieview的字体颜色
        NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
        attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
        [naviBar setTitleTextAttributes:attrs];
         */
    }
    return _imagePicker;
}

上一篇 下一篇

猜你喜欢

热点阅读