
镇楼专业
系统SDK介绍
- 打开相册选择图片
- 打开相册选择视频
- 打开相机拍摄图片
- 打开相机拍摄视频
配置权限:
在info.plist文件中添加需要的权限
- 相机权限:Privacy - Camera Usage Description 允许此权限才能使用相机功,这样才能录制视频,并且想要保存图片。
- 相册权限:Privacy - Photo Library Usage Description 允许此权限才能使用系统相册。
- 麦克风权限:Privacy - Microphone Usage Description 获取麦克风权限不然会崩,只有允许此权限才能录音。
判断是否权限
#pragma mark - 权限判断
- (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, // 打开摄像机
};
接口文件
#import <Foundation/Foundation.h>
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
实现文件
#import "LZSystemPhotoSelector.h"
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <MobileCoreServices/MobileCoreServices.h>
#define kRootViewController UIApplication.sharedApplication.delegate.window.rootViewController
@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;
}
#pragma mark - 选择图片或视频
- (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];
}
#pragma mark - 打开相机或摄像机
- (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];
}
#pragma mark - UIImagePickerControllerDelegate
- (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];
}
#pragma mark - 权限判断
- (BOOL)authorizationCamera {
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
return false;
}
return true;
}
@end
网友评论