0

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

@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 {

                    }
                }
            }

        }
    }
}

4

1 回答 1

0

在您的 ViewController 类中: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)
}
于 2022-01-03T23:52:57.503 回答