Search code examples
iosswiftuiimagepickercontroller

How to display two images on two UIImageViews one View Controller, UIImagePickerController's?


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)
    }

'''

Solution

  • 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)
        }
    
    // ...