系统SDK介绍
- 打开相册选择图片
- 打开相册选择视频
- 打开相机拍摄图片
- 打开相机拍摄视频
配置权限:
在info.plist文件中添加须要的权限
- 相机权限:Privacy - Camera Usage Description 容许此权限才能使用相机功,这样才能录制视频,而且想要保存图片。
- 相册权限:Privacy - Photo Library Usage Description 容许此权限才能使用系统相册。
- 麦克风权限:Privacy - Microphone Usage Description 获取麦克风权限否则会崩,只有容许此权限才能录音。
判断是否权限
- (BOOL)authorizationCamera {
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
return false;
}
return true;
}
复制代码
配置选项
typedef NS_ENUM(NSInteger, LZSystemPhotoSelectorType) {
LZSystemPhotoSelectorTypeVideo, // 选择视频
LZSystemPhotoSelectorTypePhoto, // 选择图片
};
typedef NS_ENUM(NSInteger, LZSystemOpenDeviceType) {
LZSystemOpenDeviceTypeCamera, // 打开相机
LZSystemOpenDeviceTypeVideo, // 打开摄像机
};
复制代码
接口文件
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger, LZSystemPhotoSelectorType) {
LZSystemPhotoSelectorTypeVideo, // 选择视频
LZSystemPhotoSelectorTypePhoto, // 选择图片
};
typedef NS_ENUM(NSInteger, LZSystemOpenDeviceType) {
LZSystemOpenDeviceTypeCamera, // 打开相机
LZSystemOpenDeviceTypeVideo, // 打开摄像机
};
@interface LZSystemPhotoSelector : NSObject
+ (instancetype)selector;
/**
选择图片或视频
@param type 类型
@param allowsEditing 是否容许编辑
@param resultFile 选择结果,图片是UIImage 视频是NSUrl
*/
- (void)lz_openAblumWithType:(LZSystemPhotoSelectorType)type allowsEditing:(BOOL)allowsEditing resultFile:(void(^)(id info))resultFile;
/**
打开相机或摄像机
@param type 类型
@param allowsEditing 是否拍摄完成进行编辑
@param resultFile 选择结果,图片是UIImage 视频是NSUrl
*/
- (void)lz_openDeviceWithType:(LZSystemOpenDeviceType)type allowsEditing:(BOOL)allowsEditing resultFile:(void(^)(id info))resultFile;
@end
NS_ASSUME_NONNULL_END
复制代码
实现文件
@interface LZSystemPhotoSelector () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, copy) void (^resultFile)(id info);
@property (nonatomic, assign) BOOL allowsEditing;
@end
@implementation LZSystemPhotoSelector
+ (instancetype)selector {
static dispatch_once_t onceToken;
static LZSystemPhotoSelector *selector;
dispatch_once(&onceToken, ^{
selector = [[LZSystemPhotoSelector alloc] init];
});
return selector;
}
- (void)lz_openAblumWithType:(LZSystemPhotoSelectorType)type allowsEditing:(BOOL)allowsEditing resultFile:(void(^)(id info))resultFile {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
resultFile(@"该设备暂时不支持");
return;
}
self.allowsEditing = allowsEditing;
self.resultFile = resultFile;
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.delegate = self;
controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if (type == LZSystemPhotoSelectorTypePhoto) {
controller.mediaTypes = [NSArray arrayWithObjects:@"public.image", nil];
} else {
controller.mediaTypes = [NSArray arrayWithObjects:@"public.movie", nil];
}
controller.allowsEditing = allowsEditing;
[kRootViewController presentViewController:controller animated:true completion:nil];
}
- (void)lz_openDeviceWithType:(LZSystemOpenDeviceType)type allowsEditing:(BOOL)allowsEditing resultFile:(void (^)(id _Nonnull))resultFile {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
resultFile(@"该设备暂时不支持");
return;
}
if (![self authorizationCamera]) {
resultFile(@"暂无相机权限");
return;
}
self.allowsEditing = allowsEditing;
self.resultFile = resultFile;
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.delegate = self;
controller.sourceType = UIImagePickerControllerSourceTypeCamera;
if (type == LZSystemOpenDeviceTypeCamera) {
controller.mediaTypes = [NSArray arrayWithObjects:@"public.image", nil];
} else {
controller.mediaTypes = [NSArray arrayWithObjects:@"public.movie", nil];
}
controller.allowsEditing = allowsEditing;
[kRootViewController presentViewController:controller animated:true completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
if (self.allowsEditing) {
UIImage *editorImage = info[UIImagePickerControllerEditedImage];
self.resultFile(editorImage);
} else {
UIImage *editorImage = info[UIImagePickerControllerOriginalImage];
self.resultFile(editorImage);
}
} else {
NSURL *url = info[UIImagePickerControllerMediaURL];
self.resultFile(url);
}
[picker dismissViewControllerAnimated:true completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:true completion:nil];
}
- (BOOL)authorizationCamera {
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
return false;
}
return true;
}
@end
复制代码