我的视图有四个按钮。当您单击某个按钮时,它将启动允许用户从其设备中选择图像的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 {
}
}
}
}
}
}发布于 2022-01-03 23:52:57
在您的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)
}https://stackoverflow.com/questions/70568583
复制相似问题