如何在两个 UIImageViews 一个 View Controller,UIImagePickerController 上显示两个图像?

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

我想在点击两个不同的 UIImageView 时从 phone 库中选取图像,并在选择后将它们显示在两个不同的 UIImageView 上, 但是我在 运行 下面的代码中,同一张图片显示在两个不同的 UIImageViews 上,我该如何解决? '''

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

'''

您观察到,当用户点击任何图像视图(wallpaperPhotoprofilePhoto)时,UIImagePickerController 始终使用 self 作为其委托.然后,当用户选择图像时,代理无法再区分最初点击的是哪个图像视图。

您可以简单地添加一个弱可选变量来指示 "active" 点击的图像视图,然后仅设置它的图像。此外,您可以使用 sender 参数(用户点击的图像视图)将点击处理程序缩减为单个函数,并将 activeImageView 设置为此。

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

// ...