OC相机封装

时间:2021-04-11 19:29:48

OC相机封装

AlbumService询问相机权限

AuthorizationService相机封装

plist文件设置:

<key>NSCameraUsageDescription</key>
<string>App需要您的同意,才能访问相机</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>App需要您的同意,才能访问相册</string>

代码:AlbumService.h

 
#import "AuthorizationService.h"
#import<AssetsLibrary/AssetsLibrary.h>
#import<CoreLocation/CoreLocation.h>
#import<AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
@implementation AuthorizationService
/**
判断是否有相机权限
*/
+ (BOOL)isHaveCameraAuthor{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if (authStatus ==AVAuthorizationStatusRestricted ||//此应用程序没有被授权访问的照片数据。可能是家长控制权限
authStatus ==AVAuthorizationStatusDenied) //用户已经明确否认了这一照片数据的应用程序访问 {
// 无权限 引导去开启
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIApplication sharedApplication]canOpenURL:url]) { [[UIApplication sharedApplication]openURL:url]; }
return NO;
}
return YES;
}
/**
判断是否有相册访问权限
*/
+ (BOOL)isHavePhotoAuthor{
ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus]; if (author ==kCLAuthorizationStatusRestricted || author ==kCLAuthorizationStatusDenied){
//无权限 引导去开启
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
} return NO; }
return YES;
}
@end

AlbumService.h

#import <Foundation/Foundation.h>

@interface AlbumService : NSObject
@property (nonatomic,assign) UIImagePickerControllerSourceType sType;
@property (nonatomic,strong) UIImage *selectedImage;
@property (nonatomic,assign) id imageStateKey;
@property (nonatomic,strong) void (^doneSelectBlock)(UIImage *image);
+(AlbumService *)sharedContactsService;
-(void)showAlbumInViewController:(UIViewController *)curVc;;
-(void)hideAlbum;
@end

AlbumService.m

#import "AlbumService.h"

@interface AlbumService ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
UIImagePickerController *imagePicker;
} @end @implementation AlbumService
static AlbumService *contacts;
+(AlbumService *)sharedContactsService{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
contacts = [AlbumService new];
});
return contacts;
} #pragma mark - setter
-(void)setSType:(UIImagePickerControllerSourceType)sType
{
_sType = sType;
if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
_sType = UIImagePickerControllerSourceTypePhotoLibrary;
}
} #pragma mark - public
-(void)showAlbumInViewController:(UIViewController *)curVc
{
UIImagePickerControllerSourceType sourceType = self.sType?:UIImagePickerControllerSourceTypePhotoLibrary;
if (!imagePicker) {
imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
}
imagePicker.sourceType = sourceType; [curVc presentViewController:imagePicker animated:YES completion:nil]; } -(void)hideAlbum
{
[imagePicker dismissViewControllerAnimated:YES completion:nil];
} #pragma mark - UIImagePickerViewDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *stateKey = self.imageStateKey?:UIImagePickerControllerEditedImage;
UIImage *resultImage = [info valueForKey:stateKey]; __weak typeof(self) weakSelf = self;
if (weakSelf.doneSelectBlock) {
weakSelf.doneSelectBlock(resultImage);
} [picker dismissViewControllerAnimated:YES completion:nil];
} -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
@end

使用

 AlbumService *service = [AlbumService sharedContactsService];

/*打开相册*/
if([AuthorizationService isHavePhotoAuthor]){//是否有相册权限
service.sType = UIImagePickerControllerSourceTypePhotoLibrary;
}else{
return;
} /*打开相机*/
if([AuthorizationService isHaveCameraAuthor]){{//是否有相机权限
service.sType = UIImagePickerControllerSourceTypeCamera;
}else{
return;
} __block typeof(self)weakSelf = self;
service.doneSelectBlock = ^(UIImage *img){
//回调的UIImage
_choseImg = img; };
[service showAlbumInViewController:self];