iOS 系统定制相机UIImagePickerController

0.Info.plist-privavy(获取用户隐私权限)

xcode删除隐私权限缓存Info.plist

  • 从相册中获取照片不需要在Info.plist中添加privacy字段获取用户授权,但跳到相册后需要用户主动选照片
  • 拍照的时候需要添加NSCameraUsageDescription字段向
  • 相册里存照片或视频时需要添加NSPhotoLibraryAddUsageDescription字段
  • 录像的时候需要添加NSMicrophoneUsageDescription字段
  • 如下图:

在这里插入图片描述
在这里插入图片描述

  • 在Info.plist中没有添加相关字段会报如下的错误:

This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app’s Info.plist must contain an NSPhotoLibraryAddUsageDescription key with a string value explaining to the user how the app uses this data.

1.示例代码

//
//  ViewController.m
//  autoCamera
//
//  Created by macvivi on 2020/6/4.
//  Copyright © 2020 macvivi. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIImagePickerControllerDelegate>

// 相机控制器
@property(strong,nonatomic)UIImagePickerController *pickController;

@property (strong, nonatomic) IBOutlet UIImageView *imageView;


@end

@implementation ViewController

//点击button后跳到相机控制器
- (IBAction)shootBtnClick:(UIButton *)sender {
    
    //    推出UIImagePickerController的时候只能present, push的话会崩溃
    [self presentViewController:self.pickController animated:YES completion:nil];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setCamera];
    
}

- (void)setCamera{
    self.pickController = [[UIImagePickerController alloc]init];
    self.pickController.delegate = self;         //代理设置
    
    //    self.pickController.mediaTypes
    //指定媒体类型是什么 照片还是视频
    //默认为 照片
    //通过下一行方法可以返回支持的类型
    //    [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    //查到很多资料都是"kUTTypeMovie","kUTTypeImage"这两个参数名称但是我测试后发现已经变成下面这两种名称
    //"public.image"  照片
    //"public.movie"  视频
    //如果全部支持可以这么设置
    //    self.pickController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    //单个支持
    //    self.pickController.mediaTypes = @[@"public.image"];
    self.pickController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    
    
    //    1.UIImagePickerControllerSourceTypePhotoLibrary:表示从照片库/集选取照片或视频  默认值
    //    2.UIImagePickerControllerSourceTypeCamera:表示从摄像头选取照片或视频(也就是拍照或录制视频)
    //    3.UIImagePickerControllerSourceTypeSavedPhotosAlbum:表示仅仅从相册中选取照片或视频
    self.pickController.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    
}

//用户选好照片或视频后会调用这个代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    
    NSLog(@"拍照,录像,或选好录像,视频的回调");
    NSLog(@"info:%@",info);
    
    //查看是视频还是照片  public.image 或 public.movie
    NSString * mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:@"public.image"]) {
        NSLog(@"照片");
        //获取拍到的照片
        UIImage* originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
        //把照片存入相册
        UIImageWriteToSavedPhotosAlbum(originalImage, nil, nil, nil);
       
    }else if ([mediaType isEqualToString:@"public.movie"]){
        NSLog(@"视频");
        //获取视频地址
        NSURL *mediaUrl = [info objectForKey:UIImagePickerControllerMediaURL];
        NSString *urlPath = [mediaUrl path];
        //保存视频到相册
        UISaveVideoAtPathToSavedPhotosAlbum(urlPath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
        
    }
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


//用户点取消后会调用这个代理方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    NSLog(@"取消");
}

//保存好视频后的回调方法
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    NSLog(@"videoPath:%@",videoPath);
    NSLog(@"error:%@",error);
    NSLog(@"contextInfo:%@",contextInfo);
}


@end


2.UIImagePickerControllerSourceTypePhotoLibrary和UIImagePickerControllerSourceTypeSavedPhotosAlbum的区别(效果图)

UIImagePickerControllerSourceTypePhotoLibrary(有分类和目录,如下图)

在这里插入图片描述

UIImagePickerControllerSourceTypeSavedPhotosAlbum(没有有分类和目录,如下图)

在这里插入图片描述

3.拍照(演示图和打印)

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

4.录像(演示图和打印)

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

参考博客:

iOS UIImagePickerController轻松调用相机详细介绍

UIImagePickerControllerSourceType 指定的几种图片的来源

UIImagePickerController拍照/相册/录像/本地视频

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页