2019-07-15 回调相册和相机

签订协议 UIImagePickerControllerDelegate,UINavigationControllerDelegate

UITapGestureRecognizer *clickTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseImage)];

   [imgV addGestureRecognizer:clickTap];// 把这个加入到图片里面(UIImageView)

// 相机事件

-(void)chooseImage{

    if(!self.imagePicker) {

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

        self.imagePicker.delegate = self;

        self.imagePicker.allowsEditing = YES;

    }

    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

     if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){

    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"从相机拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

   self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

   dispatch_async(dispatch_get_main_queue(), ^{

            [self presentViewController:self.imagePicker animated:YES completion:nil];

        });

    }];

         [actionSheetaddAction:cameraAction];

     }

    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        dispatch_async(dispatch_get_main_queue(), ^{

            [self presentViewController:self.imagePicker animated:YES completion:nil];

        });

    }];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        NSLog(@"点击了取消");

    }];

    [actionSheetaddAction:photoAction];

    [actionSheetaddAction:cancelAction];

    dispatch_async(dispatch_get_main_queue(), ^{

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

    });

}

两个类方法

//获取选择的图片

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [pickerdismissViewControllerAnimated:YES completion:nil];

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    imgV.image= image;

}

//从相机或者相册界面弹出

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

    [pickerdismissViewControllerAnimated:YES completion:nil];

}

你可能感兴趣的:(2019-07-15 回调相册和相机)