首先,在Info.plist文件中添加下面兩個key,根據(jù)app具體需求設(shè)置相應(yīng)的文案:
Privacy - Camera Usage Description
Privacy - Photo Library Usage Description
在main.storyBoard里拖一個imageView并連線,用來顯示拍攝或從相冊中選取的圖片:
調(diào)用系統(tǒng)相機(jī)垄开,引入頭文件:
#import <AVFoundation/AVFoundation.h>
調(diào)用系統(tǒng)相冊,引入頭文件:
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
遵循代理,設(shè)置全局變量并初始化:
@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *avatar;
@property (nonatomic, strong) UIImagePickerController *imagePickerController;
- (void)viewDidLoad {
[super viewDidLoad];
//為imageView添加點(diǎn)擊事件:
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;
}
點(diǎn)擊imageView逗余,彈出系統(tǒng)彈框:
- (void)tapAvatar {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *camera = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self checkCameraPermission];//檢查相機(jī)權(quán)限
}];
UIAlertAction *album = [UIAlertAction actionWithTitle:@"Album" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self checkAlbumPermission];//檢查相冊權(quán)限
}];
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 {
//判斷相機(jī)是否可用,防止模擬器點(diǎn)擊【相機(jī)】導(dǎo)致崩潰
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.imagePickerController animated:YES completion:^{
}];
} else {
NSLog(@"不能使用模擬器進(jìn)行拍照");
}
}
#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:@"請?jiān)谠O(shè)置中打開相冊" 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;
}