Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Using multiple tap gesture recognizers with imagePickerController

Hi,

I'm trying to upload two different images on a single view to a cloud storage. For this i have created two imagesViews and two tap gesture recognizers. I'm stuck in identifying the clicked tap gesture recognizer. Anyone able to help me out? I've posted the code below. The upload is working but both functions are calling the same imagePickerController but i need to have parts of the code in the imagePickerController different.

Thanks, Patrick

@IBAction func handleProfileImageUpload(sender: AnyObject) {
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.allowsEditing = true
        presentViewController(picker, animated: true, completion: nil)
}

@IBAction func handleProfileTitleImageUpload(sender: AnyObject) {
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.allowsEditing = true
        presentViewController(picker, animated: true, completion: nil)
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        dismissViewControllerAnimated(true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        var selectedImageFromPicker: UIImage?

        if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
            selectedImageFromPicker = editedImage
        } else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage     {
            selectedImageFromPicker = originalImage
        }
.........

1 Answer

I hope I understand your problem correctly and not simplifying it too much: you can use a class variable and set it to a different value in each IBAction function (handleProfileTitleImageUpload or handleProfileImageUpload) before you show the picker dialog.. then inside the picker didFinishPickingMediaWithInfo function you can check which value does the variable have and decide what to do with the image. ''' var callingGesture: Int = 0

@IBAction func handleProfileImageUpload(sender: AnyObject) {

    let picker = UIImagePickerController()
  picker.delegate = self
    picker.delegate = self
    picker.allowsEditing = true
    callingGesture = 1
    presentViewController(picker, animated: true, completion: nil)
}

@IBAction func handleProfileTitleImageUpload(sender: AnyObject) {

    let picker = UIImagePickerController()
    picker.delegate = self
    picker.allowsEditing = true
    callingGesture = 2
    presentViewController(picker, animated: true, completion: nil)
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    dismissViewControllerAnimated(true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    var selectedImageFromPicker: UIImage?

    if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
        selectedImageFromPicker = editedImage
    } else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage     {
        selectedImageFromPicker = originalImage
    }
picker.dismissViewControllerAnimated(true, completion: nil)
    if callingGesture == 1 {
        // do this 
    } else if callingGesture == 2 {
        //do that
    }
}

'''

Great, that was easy :) thank you!