Search code examples
iosswiftuiimageviewuiimagepickercontroller

How to differentiate which UIImagePickerController calling the func didCancel if I have two image views on same view?


I have two UIImageViews (leftImageView & rightImageView) on same view. I'm trying to know which UIImagePickerController is calling the imagePickerControllerDidCancel. Is it the leftImageView or the right one?

Here's the switch for the picker buttons to pick the left or right image.

switch sender.tag {
    // left image
case 0:
    self.imageView = leftImage
default:
    self.imageView = rightImage
}

The func below is able to know which image view is calling it. But the didCancel func does not:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let image = info[.originalImage] as? UIImage {
        self.imageView.image = image
    }
    picker.dismiss(animated: true, completion: nil)
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 
    // the rightImageView or the leftImageView calling the cancel?

    if (rightImageView){
        // print("the right image ")
    }

    picker.dismiss(animated: true, completion: nil)
}

Solution

  • You already have the info you need. Check self.imageView:

    if self.imageView === leftImageView {
        // it's the left image view
    } else {
        // it's the right image view
    }