【问题标题】:How to display two images on two UIImageViews one View Controller, UIImagePickerController's? [duplicate]如何在两个 UIImageViews 一个 View Controller,UIImagePickerController 上显示两个图像? [复制]
【发布时间】:2020-02-19 16:16:32
【问题描述】:

当我点击两个不同的 UIImageView 并选择后,我想从手机库中选择图像,然后将它们显示在两个不同的 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)
    }

'''

【问题讨论】:

    标签: ios swift uiimagepickercontroller


    【解决方案1】:

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

    您可以简单地添加一个弱可选变量来指示“活动”点击的图像视图,然后只设置它的图像。此外,您可以使用 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)
        }
    
    // ...
    

    【讨论】:

    • 能否请您更明确地解释一下我的代码?我应该在我的代码中添加什么以及在哪里添加?
    • 当我尝试弱 var activeImageView:UIImageView? = nil,如果我在类中添加它并运行代码,它会给我致命错误:Unexpectedly found nil while unwrapping an Optional value: file on line activeImageView.image = image
    • 我明白了。您需要将变量放入“主”类定义 (class SettingProfileViewController) 中,靠近声明插座的位置。
    • 不断给出错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UITapGestureRecognizer setImage:]:无法识别的选择器发送到实例 0x600003515400”
    • 对不起,我的错。 sender 是手势识别器,所以你需要访问sender.view 来获取接收到手势的图像视图。查看我更改的handleSelect 函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 2014-02-12
    • 2014-03-26
    • 2016-12-25
    • 2018-10-04
    • 1970-01-01
    相关资源
    最近更新 更多