首先,在Info.plist文件中添加下面两个key,根据app具体需求设置相应的文案:
Privacy - Camera Usage Description
Privacy - Photo Library Usage Description
在main.storyBoard里拖一个imageView并连线,用来显示拍摄或从相册中选取的图片:
调用系统相机,引入头文件:
#import <AVFoundation/AVFoundation.h>
调用系统相册,引入头文件:
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
遵循代理,设置全局变量并初始化:
@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *avatar;
@property (nonatomic, strong) UIImagePickerController *imagePickerController;
- (void)viewDidLoad {
[super viewDidLoad];
//为imageView添加点击事件:
self.avatar.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAvatar)];
[self.avatar addGestureRecognizer:tap];
}
- (UIImagePickerController *)imagePickerController {
if (_imagePickerController == nil) {
_imagePickerController = [[UIImagePickerController alloc] init];
_imagePickerController.delegate = self; //delegate遵循了两个代理
_imagePickerController.allowsEditing = YES;
}
return _imagePickerController;
}
点击imageView,弹出系统弹框:
- (void)tapAvatar {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *camera = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self checkCameraPermission];//检查相机权限
}];
UIAlertAction *album = [UIAlertAction actionWithTitle:@"Album" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self checkAlbumPermission];//检查相册权限
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:camera];
[alert addAction:album];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
#pragma mark - Camera
- (void)checkCameraPermission {
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (status == AVAuthorizationStatusNotDetermined) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
[self takePhoto];
}
}];
} else if (status == AVAuthorizationStatusDenied || status == AVAuthorizationStatusRestricted) {
[self alertCamear];
} else {
[self takePhoto];
}
}
- (void)takePhoto {
//判断相机是否可用,防止模拟器点击【相机】导致崩溃
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.imagePickerController animated:YES completion:^{
}];
} else {
NSLog(@"不能使用模拟器进行拍照");
}
}
#pragma mark - Album
- (void)checkAlbumPermission {
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusNotDetermined) {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
dispatch_async(dispatch_get_main_queue(), ^{
if (status == PHAuthorizationStatusAuthorized) {
[self selectAlbum];
}
});
}];
} else if (status == PHAuthorizationStatusDenied || status == PHAuthorizationStatusRestricted) {
[self alertAlbum];
} else {
[self selectAlbum];
}
}
- (void)selectAlbum {
//判断相册是否可用
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:self.imagePickerController animated:YES completion:^{
}];
}
}
- (void)alertAlbum {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"请在设置中打开相册" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
UIImagePickerControllerDelegate代理方法:
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
self.avatar.image = image;
}