iOS相册访问
#import <AVFoundation/AVFoundation.h>
#import <Photos/PHPhotoLibrary.h>
//获取相册访问权限
PHAuthorizationStatus authorization = [PHPhotoLibrary authorizationStatus];
if (authorization == PHAuthorizationStatusDenied) {
//修改设置当中权限
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}else if(authorization == PHAuthorizationStatusNotDetermined){
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if(status == PHAuthorizationStatusAuthorized) {
[self presentImageControllerWithType:ImagePickerTypePhoto];
}
}];
}else if (authorization == PHAuthorizationStatusAuthorized) {
[self presentImageControllerWithType:ImagePickerTypePhoto];
}
-(void)openCamera{
SKLog(@"openCamera");
AVAuthorizationStatus authorization = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authorization == AVAuthorizationStatusDenied) {
//修改设置当中权限
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}else if (authorization == AVAuthorizationStatusNotDetermined){
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
[self presentImageControllerWithType:ImagePickerTypeCamera];
}
}];
}else if (authorization == AVAuthorizationStatusAuthorized){
[self presentImageControllerWithType:ImagePickerTypeCamera];
}
}
- 获取权限后调用UIImagePickerController
-(void)presentImageControllerWithType:(ImagePickerType)pickerType{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.view.backgroundColor = [UIColor whiteColor];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
switch (pickerType) {
//判断当前相册是否可用
case ImagePickerTypePhoto:{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self.navigationController presentViewController:imagePicker animated:YES completion:^{
}];
}else{
[MBProgressHUD showError:@"抱歉,无法访问相册"];
}
}
break;
//判断摄像头是否可用
case ImagePickerTypeCamera:{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {//后置摄像头可用
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
[self.navigationController presentViewController:imagePicker animated:YES completion:^{
}];
}else if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]){//前置摄像头可用
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
[self.navigationController presentViewController:imagePicker animated:YES completion:^{
}];
}else{
[MBProgressHUD showError:@"前后摄像头已损坏,无法使用"];
}
}else{
[MBProgressHUD showError:@"抱歉,相机无法使用"];
}
}
break;
default:
break;
}
}
- UIImagePickerController delegate
//图片选择代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//获取图片信息
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
[self dismissViewControllerAnimated:YES completion:nil];
}
//取消事件代理
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}