在Image View上添加点击手势Tap Gesture Recognizer
?给点击手势添加触发函数,拖拽点击手势至View Controller
函数名称Name:tapImageView ? ? ? ?Type:UITapGestureRecognizer
默认情况下UIImage View不会响应手势,修改Image View属性,勾选User Interaction Enabled,启用用户交互
?UIImagePickerController是ios系统提供的相册选图控制器
@IBAction func tapImageView(_ sender: UITapGestureRecognizer) {
//print(#function)
let picker = UIImagePickerController()
picker.delegate = self
present(picker, animated: true, completion: nil)
}
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
imageView.image = info[.originalImage] as? UIImage
}
}
当前控制器需要遵循的协议(可选的):UIImagePickerControllerDelegate, UINavigationControllerDelegate
imagePickerController(){...}函数:选好图片会调用函数
picker.dismiss(animated: true, completion: nil):选图页面消失
|