@interface MainViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:button];
button.frame = CGRectMake(20, 100, self.view.bounds.size.width-40, 40);
[button setTitle:@"相册图片" forState:UIControlStateNormal];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:button1];
button1.frame = CGRectMake(20, 200, self.view.bounds.size.width-40, 40);
[button1 setTitle:@"相机图片" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)click:(UIButton *)but
{
[self pickImageFromAlbum];
}
- (void)click1:(UIButton *)but
{
[self pickImageFromCamera];
}
//相册
- (void)pickImageFromAlbum
{
UIImagePickerController *imagepicker = [[UIImagePickerController alloc]init];
imagepicker.delegate = self;
imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagepicker.allowsEditing = YES;
[self presentViewController:imagepicker animated:YES completion:^{
}];
}
//摄像头
- (void)pickImageFromCamera
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:^{
}];
}
//选完照片的回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
//检测是不是原始照片
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//检测是不是编辑过的照片
UIImage *image1 = [info objectForKey:UIImagePickerControllerEditedImage];
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
}
if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
}
[self dismissViewControllerAnimated:YES completion:^{
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:^{
}];
}