来自 PHPickerViewController 的多个按钮图像

Multiple Button Images from PHPickerViewController

我的视图有四个按钮。当您单击一个按钮时,它会启动 PHPickerViewController,允许用户从他们的设备 select 图像。我如何知道点击了哪个按钮,以便我可以在该特定按钮上设置所选图像?

@objc func showImage(sender: UIButton){
    var configuration1 = PHPickerConfiguration(photoLibrary: .shared())
    configuration1.selectionLimit = 1
    configuration1.filter = .images
    let picker = PHPickerViewController(configuration: configuration1)
    picker.delegate = self
    present(picker, animated: true, completion: nil)

}
extension ProfileController: PHPickerViewControllerDelegate {
    func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        dismiss(animated: true, completion: nil)
        guard !results.isEmpty else {
            return
        }
        for result in results {
            result.itemProvider.loadObject(ofClass: UIImage.self) {
                [weak self]
                object, error in
                DispatchQueue.main.async {
                    guard let self = self else {
                        return
                    }
                    if let image = object as? UIImage {

                    }
                }
            }

        }
    }
}

在你的 ViewController class 中:var selectedButton: UIButton!

@objc func showImage(sender: UIButton) {
  selectedButton = sender

  var configuration1 = PHPickerConfiguration(photoLibrary: .shared())
  // rest of your code
}

然后在你的 didFinishPicking 委托中:

if let image = object as? UIImage {        
  selectedButton.setImage(image, for: .normal)
}