1.首先在storyBoard中创建一个button和imageView并添加约束
添加代理方法:
@interface ViewController ()<UINavigationControllerDelegate,
UIImagePickerControllerDelegate>
从storyBoard--imageView中拖拽出来的属性
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
//从storyBoard--button拖拽出来的点击方法
- (IBAction)didClickedPickImageButton:(UIButton *)sender {
//创建UIImagePickerController
UIImagePickerController * imagePicker = [[UIImagePickerController alloc]init];
imagePicker.editing = YES;
imagePicker.allowsEditing = YES;
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
(从系统相册中选取照片)
//模态出相册界面
[self presentViewController:imagePicker animated:YES completion:nil];
}
//使用选取的图片:
-(void)imagePickerController:(UIImagePickerController *)
picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
UIImage * selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
self.imageView.image = selectedImage;
}