UIImagePickerController 拍照

禹昆
2023-12-01

@interface TestAlertViewController ()<UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>


@property (nonatomic,strong ) IBOutlet UIImageView *phonoImage;

@property (nonatomic,strong ) UIImagePickerController *imagePikerViewController;



@end


@implementation TestAlertViewController


- (void)viewDidLoad {

    [super viewDidLoad];


    self.imagePikerViewController = [[UIImagePickerController alloc]init];

    self.imagePikerViewController.delegate = self;

    self.phonoImage.contentMode = UIViewContentModeScaleAspectFit;


}


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{


    UIImage *image = info[UIImagePickerControllerEditedImage];

    if (!image) {

        image = info[UIImagePickerControllerOriginalImage];

    }

    self.phonoImage.image = image;

    [self dismissViewControllerAnimated:YES completion:NULL];


}


-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{


    [self dismissViewControllerAnimated:YES completion:NULL];

}



- (IBAction)alertButton:(UIButton *)sender {


    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];

    [alertController addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {


     //处理拍照

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {


            self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypeCamera;

            [self presentViewController:self.imagePikerViewController animated:YES completion:NULL];


        }


    }]];


    [alertController addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {


    //从相册中选取

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){

            self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

            [self presentViewController:self.imagePikerViewController animated:YES completion:NULL];

        }



    }]];

    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {


    }]];


    [self presentViewController:alertController animated:YES completion:nil];


}


 类似资料: