创建UIImagePickerController对象 定以属性
@property(nonatomic, strong)UIImagePickerController *imagePickerController; //选择相册
//设置相册
- (void)setpImagePicker
{
_imagePickerController = [[UIImagePickerController alloc] init];
_imagePickerController.delegate = self;
_imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
_imagePickerController.allowsEditing = YES;
}
设置获取图片的来源 , 如相机,或者相册, 以下代码是直接从相册选择 在你的单击事件中调用此方法
#pragma mark ----- 从相册获取图片
- (void)selectImageFromAlbum
{
_imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:_imagePickerController animated:YES completion:nil];
}
实现UIImagePickerControllerDelegate, UINavigationControllerDelegate代理方法
#pragma mark ------- 选择图片后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
//如果是图片
UIImage *image = info[UIImagePickerControllerEditedImage];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
如果遇到权限问题请访问: