如何使用Tap Gesture从UIImageView呈现UIImagePickerController

时间:2015-10-02 14:09:31

标签: ios swift uiimageview uiimagepickercontroller uitapgesturerecognizer

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

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

    }
}

2 个答案:

答案 0 :(得分:1)

尝试在userInteractionEnabled上启用UIImageView

profilePicture.userInteractionEnabled = true

答案 1 :(得分:0)

我倾向于在图像上放置一个看不见的UIButton。最简单/最简单的方法,不会过度复杂化。