背景:iOS调用相机和访问图库
一、调用相机或图库:
-(void)imgviewClick
{
ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
UIAlertController *alertvc=[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cameraAction =[UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *picker=[[UIImagePickerController alloc]init];
if(authStatus == ALAuthorizationStatusRestricted || authStatus == ALAuthorizationStatusDenied){
[self resultHud:@"请在设置中打开您的相机权限!"];
// // 无权限 引导去开启相机
// NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
// if ([[UIApplication sharedApplication]canOpenURL:url]) {
// [[UIApplication sharedApplication]openURL:url];
return;
}
else{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
picker.sourceType=UIImagePickerControllerSourceTypeCamera;
picker.editing=YES;
picker.allowsEditing=YES;
picker.delegate=self;
[self presentViewController:picker animated:YES completion:nil];
}
else
{
[self resultHud:@"未检测到相机!"];
}
}
}];
UIAlertAction *libraryAction =[UIAlertAction actionWithTitle:@"照片图库" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *picker=[[UIImagePickerController alloc]init];
if(author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied){
[self resultHud:@"请在设置中打开您的相册权限!"];
// NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
// if ([[UIApplication sharedApplication] canOpenURL:url]) {
// [[UIApplication sharedApplication] openURL:url];
// }
return;
}
else{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
picker.editing=YES;
picker.allowsEditing=YES;
picker.delegate=self;
[self presentViewController:picker animated:YES completion:nil];
}
else
{
[self resultHud:@"未检测到图库!"];
}
}
}];
UIAlertAction *cancleAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertvc addAction:libraryAction];
[alertvc addAction:cameraAction];
[alertvc addAction:cancleAction];
[self presentViewController:alertvc animated:YES completion:nil];
}
⚠️注意:1、为什么打开图库后 或者打开相机操作完之后,一直不会返回你原先的界面: [self presentViewController:alertvc animated:YES completion:nil];
因为你需要将pickerviewcontroller dimiss掉,pickerviewcontroller 的调用就是通过原控制器调用present方法实现的,所以可以用dismiss关闭
2、灰色部分表示,直接调用应用程序的设置界面,带领用户去开启权限
二、编辑选择的图片并关闭图像选择控制器
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
NSLog(@"info--->%@",info);
UIImage *originaImage =[info objectForKey:UIImagePickerControllerEditedImage];
_headimgView.image=originaImage;
originaImage=[self imageWithImage:originaImage scaledToSize:CGSizeMake(IMG_SECTION_HEIGHT,IMG_SECTION_HEIGHT)];
[self dismissViewControllerAnimated:YES completion:nil];
}
可以在图像编辑中完成你之后想借用相机选择图片之后,要实现的例如上传图像等操作
⚠️注意:我这里打印出来的info 就是图像选择器完成之后,返回的数据,根据打印出来的数据你可以发现,返回的图像有两种类型
1、UIImagePickerControllerEditedImage 2、UIImagePickerControllerOriginaImage
UIImagePickerControllerEditedImage 这个代表你在图像选择器中选择的图片在选择框中是怎么样的返回的就是怎么样的
UIImagePickerControllerOriginaImage 这个表示,不管你在图像选择器的选择框内将图片是方法还是缩小,返回的都是原图,未经过任何操作。