调用系统相机/相册代码片段

1
2
3
4
5
6
7
[self showActionSheet:@[@"拍摄/上传",@"从相册中选择照片"] callBackBlock:^(NSString *item) {
if ([item isEqualToString:@"拍摄/上传"]) {
[self takePhotoWithCamer];
}else if ([item isEqualToString:@"从相册中选择照片"]) {
[self choosePhotoFromLibrary];
}
}];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//弹出选项
- (void)showActionSheet:(NSArray *)items callBackBlock:(void(^)(NSString *item))callBackBlock
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancelAction];

for (NSString *item in items) {
UIAlertAction *action = [UIAlertAction actionWithTitle:item style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if (callBackBlock) {
callBackBlock(item);
}
}];
[alertController addAction:action];
}
[self presentViewController:alertController animated:YES completion:nil];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//使用相机拍照
-(void)takePhotoWithCamer{
if([self isCanUseCamera]){
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.showsCameraControls = NO;

UIView * customCameraView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIButton * takePicture = [UIButton buttonWithType:UIButtonTypeCustom];
takePicture.frame = CGRectMake(0, SGFullScreenHeight-44, SGFullScreenWidth, 44);
[takePicture setTitle:@"照相" forState:UIControlStateNormal];
[takePicture sg_handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
[imagePicker takePicture];
}];
[customCameraView addSubview:takePicture];
imagePicker.cameraOverlayView = customCameraView;

CGSize screenSize = [[UIScreen mainScreen] bounds].size;
float aspectRatio = 4.0/3.0;
float scale = screenSize.height/screenSize.width * aspectRatio;
imagePicker.cameraViewTransform = CGAffineTransformMakeScale(scale, scale);
[self presentViewController:imagePicker animated: YES completion: nil];
}else{
//@"无法使用相机, 请到 系统设置 -> 隐私 -> 相机 里启动权限"
}
}


//判断相机是否可用
-(BOOL)isCanUseCamera{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied){
return NO;
}else{
return YES;
}
}

//从相册选照
-(void)choosePhotoFromLibrary{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.view.tintColor = self.view.tintColor;
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePicker.allowsEditing = NO;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated: true completion: nil];
}
1
2
3
4
5
6
7
8
9
10
11
12
//选照完成
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *pickedImage = (UIImage *)info[UIImagePickerControllerOriginalImage];
//...
[self dismissViewControllerAnimated:YES completion:nil];

}

//取消选择
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}