0

I want to pick images from phone library when I tap on two different UIImageViews and after selection, display them on two different UIImageView's, but I when run the following code, the same image displays at two different UIImageViews, How can I fix it? '''

extension SettingProfileViewController:UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
           profilePhoto.image = image
            print("profile")
        }

        if let wallImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            wallpaperPhoto.image = wallImage
            print("wallpaper")
        }


        dismiss(animated: true, completion: nil)

    }

}
override func viewDidLoad() {
        super.viewDidLoad()
     let tapGesture = UITapGestureRecognizer(target: self, action: #selector(SettingProfileViewController.handleSelectProfilePhotoView))
        profilePhoto.addGestureRecognizer(tapGesture)
        profilePhoto.isUserInteractionEnabled = true

     let wallTapGesture = UITapGestureRecognizer(target: self, action: #selector(SettingProfileViewController.handleSelectWallpaperImageView))
        wallpaperPhoto.addGestureRecognizer(wallTapGesture)
        wallpaperPhoto.isUserInteractionEnabled = true
}

 @objc func handleSelectProfilePhotoView(){
        let pickerController = UIImagePickerController() //открывает галерею
        pickerController.delegate = self
        present(pickerController, animated: true, completion: nil)
    }


    @objc func handleSelectWallpaperImageView(){
        let pickerCont = UIImagePickerController()
        pickerCont.delegate = self
        present(pickerCont, animated: true, completion: nil)
    }

'''
Emtel
  • 1
  • 4

1 Answers1

0

What you observe is that when the user taps on any of the image views (wallpaperPhoto or profilePhoto), the UIImagePickerController always uses self as its delegate. Then, when the user picks an image, the delegate cannot distinguish any more which image view originally was tapped.

You could simply add a weak optional variable indicating the "active" tapped image view, and then set only it's image. Also, you can reduce the tap handler to a single function with a sender argument, which is the image view the user tapped on, and set the activeImageViewto this.

extension SettingProfileViewController:UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    weak var activeImageView:UIImageView? = nil

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


    override func viewDidLoad() {
         super.viewDidLoad()
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(SettingProfileViewController.handleSelect(_:))
            profilePhoto.addGestureRecognizer(tapGesture)
            profilePhoto.isUserInteractionEnabled = true

         let wallTapGesture = UITapGestureRecognizer(target: self, action: #selector(SettingProfileViewController.handleSelect(_:)))
            wallpaperPhoto.addGestureRecognizer(wallTapGesture)
            wallpaperPhoto.isUserInteractionEnabled = true
    }

    @objc func handleSelect(sender:UIGestureRecognizer) {
        guard let sendingImageView = sender.view as? UIImageView else {
            print("Ooops, received this gesture not from an ImageView")
            return
        }
        activeImageView = sendingImageView
        let pickerController = UIImagePickerController() //открывает галерею
        pickerController.delegate = self
        present(pickerController, animated: true, completion: nil)
    }

// ...
Andreas Oetjen
  • 9,889
  • 1
  • 24
  • 34
  • Could you please explain more explicitly relative to my code? What and where exactly should I add to my code? – Emtel Feb 19 '20 at 16:46
  • It gives me an error "Extensions must not contain stored properties", when I try to weak var activeImageView:UIImageView? = nil, and if I add it inside the class and I run the code, it gives me Fatal error: Unexpectedly found nil while unwrapping an Optional value: file on the line activeImageView.image = image – Emtel Feb 19 '20 at 18:20
  • I see. You need to put the variable into the "main" class definition (`class SettingProfileViewController`), near to where the outlets are declared. – Andreas Oetjen Feb 19 '20 at 18:56
  • keeps giving an error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITapGestureRecognizer setImage:]: unrecognized selector sent to instance 0x600003515400' – Emtel Feb 19 '20 at 19:07
  • Sorry, my fault. `sender` is the gesture recognizer, so you need to access `sender.view` to get the image view that received the gesture. See my changed `handleSelect` function – Andreas Oetjen Feb 19 '20 at 22:09
  • thank you so much. I applied a little different approach, but the used your handSelect and it works. – Emtel Feb 19 '20 at 22:36