-3

这里有 4 个 UIImageView,每个都有单独的 4 个按钮用于图像上传。当用户单击每个按钮时,我无法修复适当的图像必须上传。意味着当用户单击返回按钮时,必须上传适当的图像。我附上了我的快速代码,请帮助解决这个问题

 var imagePicker = UIImagePickerController()
 var imagePicked = 0

@IBOutlet weak var adImage1: UIImageView!
@IBOutlet weak var adImage2: UIImageView!
@IBOutlet weak var adImage3: UIImageView!
@IBOutlet weak var adImage4: UIImageView!

 @IBAction func uploadImage1(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .photoLibrary;
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)
        }
    }

    @IBAction func uploadImage2(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            let imagePicker1 = UIImagePickerController()
            imagePicker1.delegate = self
            imagePicker1.sourceType = .photoLibrary;
            imagePicker1.allowsEditing = true
            self.present(imagePicker1, animated: true, completion: nil)
        }     }
    @IBAction func uploadImage3(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            let imagePicker2 = UIImagePickerController()
            imagePicker2.delegate = self
            imagePicker2.sourceType = .photoLibrary;
            imagePicker2.allowsEditing = true
            self.present(imagePicker2, animated: true, completion: nil)
        }  }
    @IBAction func uploadImage4(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            let imagePicker3 = UIImagePickerController()
            imagePicker3.delegate = self
            imagePicker3.sourceType = .photoLibrary;
            imagePicker3.allowsEditing = true
            self.present(imagePicker3, animated: true, completion: nil)
        }  }


     internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
     {
         let imagePicker = info[UIImagePickerControllerOriginalImage] as? UIImage
        if imagePicked == 1 {
       adImage1.image = imagePicker
        }
     else if imagePicked == 2
            {
                adImage2.image = imagePicker
            }
            else if imagePicked == 3

                {
                    adImage3.image = imagePicker
                }
              else if imagePicked == 4
                    {
                        adImage4.image = imagePicker
                 }                    else
                    {
                       // Error message
}
         self.dismiss(animated: true, completion: nil)
    }
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true)
    }
4

1 回答 1

0

更改您的按钮 IBaction 代码以设置基于按钮单击选择的图像,以便您的代码像魅力一样工作

@IBAction func uploadImage1(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            imagePicked = 1
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .photoLibrary;
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)
        }
    }

    @IBAction func uploadImage2(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            imagePicked = 2
            let imagePicker1 = UIImagePickerController()
            imagePicker1.delegate = self
            imagePicker1.sourceType = .photoLibrary;
            imagePicker1.allowsEditing = true
            self.present(imagePicker1, animated: true, completion: nil)
        }
    }
    @IBAction func uploadImage3(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            imagePicked = 3
            let imagePicker2 = UIImagePickerController()
            imagePicker2.delegate = self
            imagePicker2.sourceType = .photoLibrary;
            imagePicker2.allowsEditing = true
            self.present(imagePicker2, animated: true, completion: nil)
        }
    }
    @IBAction func uploadImage4(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            imagePicked = 4
            let imagePicker3 = UIImagePickerController()
            imagePicker3.delegate = self
            imagePicker3.sourceType = .photoLibrary;
            imagePicker3.allowsEditing = true
            self.present(imagePicker3, animated: true, completion: nil)
        }
    }
于 2018-05-17T13:20:32.147 回答