ios 相机调用之读取相册

时间:2023-03-09 18:19:56
ios 相机调用之读取相册
UIIamgePickerControllerr可以从照片库中读取一张图片到咱们应用程序中来
步骤:
//创建图片判断图片库是否可以使用
if([UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary]){
1.创建一个UIImagePickerController对象
UIImagePickerController *picker=[[UIImagePickerController alloc]init];
//2.需要设置该对象拾取源属性
picker.souceType=UIImagePickerControllerSourceTypePhotoLibrary;
//3.设置代理
picker.delegate=self;
实现代理<UIImagePickerControllerDelegate,UINavigationControllerdelegate>
//4.显示,以modal的形式显示在当前控制器的view中
[self presenTViewController:picker animated:YES completion:nil];
//获取在照片库中选择图片
-(void)imagePickerController:(nonnull UIImagePickerController *)picker didFinshPickingMediaWithInfo:(nonnull NSDictionary<NSString * id>*) info
{
//获取图片
UIImage *img=info[UIImagePickeControllerOriginalImage];
self.photo.image=img;
//隐藏
[picker dismissViewControllerAnimated:YES completion:nil];
}
//点击取消
-(void)immagePickerControllerDidCancel:(nonnull UIImagePickerController *) picker
{
//隐藏
[picker dismissViewControllerAnimated:YES completion:nil];
}