如何使用点击手势从 UIImageView 显示 UIImagePickerController

How to present UIImagePickerController from UIImageView with Tap Gesture

我希望视图加载默认图像并让用户点击此图像到 select 照片库中的另一个图像。我这样做了,但是点击手势不起作用,我做错了什么?

class ProfileViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

    let imagePicker = UIImagePickerController()
    @IBOutlet var profilePicture: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let tap = UITapGestureRecognizer(target: self, action: Selector("selectProfilePicture"))
        profilePicture.addGestureRecognizer(tap)

        imagePicker.delegate = self
    }

    func selectProfilePicture() {

        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        imagePicker.allowsEditing = false
        self.presentViewController(imagePicker, animated: true, completion: nil)

    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

        self.dismissViewControllerAnimated(true, completion: nil)
        profilePicture.image = image

    }
}

尝试在您的 UIImageView:

上启用 userInteractionEnabled
profilePicture.userInteractionEnabled = true

我倾向于在图像顶部放置一个不可见的 UIButton。最简单/最简单的方法,不会过于复杂。