效果圖:
步驟:
1.配置info.plist文件中調(diào)用相機(jī)和相冊的權(quán)限
2.設(shè)置彈框添加事件
3.判斷并申請權(quán)限
4.調(diào)起系統(tǒng)相冊和相機(jī)
直接上代碼:
#import "ViewController.h"
#pragma mark - 01.使用相機(jī)相冊要導(dǎo)入頭文件的
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
#pragma mark - 02.拖線一個(gè)imageView控件用來展示選中的圖片 & 創(chuàng)建一個(gè)彈框;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic, strong) UIImagePickerController *imagePickerController;
@end
@implementation ViewController
#pragma mark - 03.懶加載初始化彈框;
- (UIImagePickerController *)imagePickerController {
if (_imagePickerController == nil) {
_imagePickerController = [[UIImagePickerController alloc] init];
_imagePickerController.delegate = self; //delegate遵循了兩個(gè)代理
_imagePickerController.allowsEditing = YES;
}
return _imagePickerController;
}
- (void)viewDidLoad {
[super viewDidLoad];
//MARK: - 04.給視圖添加手勢.(添加手勢就是租給那些自己買不起selector的控件一個(gè)觸發(fā)方法);
//首先開啟該視圖與用戶交互的開關(guān).默認(rèn)是關(guān);
//這個(gè)應(yīng)該是個(gè)輕拍手勢;
self.imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewIsSelector)];
[self.imageView addGestureRecognizer:tap];
}
#pragma mark - 05.在租來的觸發(fā)方法里添加事件;
- (void)imageViewIsSelector {
//MARK: - 06.點(diǎn)擊圖片調(diào)起彈窗并檢查權(quán)限;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *camera = [UIAlertAction actionWithTitle:@"使用相機(jī)拍攝" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self checkCameraPermission];//調(diào)用檢查相機(jī)權(quán)限方法
}];
UIAlertAction *album = [UIAlertAction actionWithTitle:@"從相冊中選擇" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self checkAlbumPermission];//調(diào)起檢查相冊權(quán)限方法
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" 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(檢查相機(jī)權(quán)限方法)
- (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 alertAlbum];//如果沒有權(quán)限給出提示
} else {
[self takePhoto];//有權(quán)限進(jìn)入調(diào)起相機(jī)方法
}
}
- (void)takePhoto {
#pragma mark - 07.判斷相機(jī)是否可用稀拐,如果可用調(diào)起
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.imagePickerController animated:YES completion:^{
}];
} else {//不可用只能GG了
NSLog(@"木有相機(jī)");
}
}
#pragma mark - Album(相冊流程與相機(jī)流程相同,相冊是不存在硬件問題的,只要有權(quán)限就可以直接調(diào)用)
- (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:@"請?jiān)谠O(shè)置中打開相冊" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
#pragma mark - UIImagePickerControllerDelegate(最后一步,選完就把圖片加上就完事了)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];
self.imageView.image = image;
}
@end
基本上就大功告成了