UIImagePickerController 拍照

来源:互联网 发布:云计算系统软件 编辑:程序博客网 时间:2024/05/14 13:27

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


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

@property (nonatomic,strong )UIImagePickerController *imagePikerViewController;



@end


@implementation TestAlertViewController


- (void)viewDidLoad {

    [superviewDidLoad];


    self.imagePikerViewController = [[UIImagePickerControlleralloc]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;

    [selfdismissViewControllerAnimated:YEScompletion:NULL];


}


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


    [selfdismissViewControllerAnimated:YEScompletion:NULL];

}



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


    UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:nilmessage:nilpreferredStyle:UIAlertControllerStyleAlert];

    [alertController addAction:[UIAlertActionactionWithTitle:@"拍照"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {


     //处理拍照

        if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {


            self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypeCamera;

            [selfpresentViewController:self.imagePikerViewControlleranimated:YEScompletion:NULL];


        }


    }]];


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


    //从相册中选取

        if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){

            self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

            [selfpresentViewController:self.imagePikerViewControlleranimated:YEScompletion:NULL];

        }



    }]];

    [alertController addAction:[UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {


    }]];


    [selfpresentViewController:alertController animated:YEScompletion:nil];


}


0 0