系統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
複製程式碼