//首选需要遵循协议
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
//如果是imageView的话, interactionEnabled必须设置为YES
self.headImageView.userInteractionEnabled = YES;
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(respondToTapGesture:)];
[self.headImageView addGestureRecognizer:tapGesture];
//手势方法
-(void)respondToTapGesture:(UITapGestureRecognizer *)gestrue{
UIImagePickerController * imageController = [[UIImagePickerController alloc]init];
imageController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//数据源类型有三种

UIImagePickerControllerSourceTypeCamera
这种方式直接打开设备相机取图

imageController.allowsEditing = YES; //设置可以编辑
imageController.delegate = self;//设定委托
[self presentViewController:imageController animated:YES completion:nil];
}
//UIImagePickerControllerDelegate 协议方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *image = info[@"UIImagePickerControllerEditedImage"];
self.headImageView.image = image;
[self dismissViewControllerAnimated:YES completion:nil];
}
UIImagePickerController 用法
最新推荐文章于 2018-07-25 14:24:07 发布
本文详细介绍了如何在iOS应用中利用UIImagePickerController实现用户通过相机拍摄或图片库选择功能来更换头像的功能。通过设置interactionEnabled属性为YES使imageView可交互,并通过UITapGestureRecognizer触发选择操作。当用户完成选择后,使用UIImagePickerControllerDelegate协议方法处理选择的图片并将其显示在用户头像上。
389

被折叠的 条评论
为什么被折叠?



