简体   繁体   English

来自 PHPickerViewController 的多个按钮图像

[英]Multiple Button Images from PHPickerViewController

My View has four buttons.我的视图有四个按钮。 When you click on a button it launches the PHPickerViewController that allows a user to select an image from their device.当你点击一个按钮时,它会启动 PHPickerViewController,它允许用户从他们的设备中 select 图像。 How can I know which button was clicked so that I can set the chosen image on that specific button.我如何知道单击了哪个按钮,以便我可以在该特定按钮上设置所选图像。

''' '''

@objc func showImage(sender: UIButton){ @objc func showImage(发件人: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 {扩展 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 {
                    
                   
                    
                   
                }
               
            }
           
           
           
           
        }
       
    }
   
}

} }

''' '''

inside your ViewController class: var selectedButton: UIButton!在你的 ViewController class: var selectedButton: UIButton!

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

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

Then in your didFinishPicking delegate:然后在您的 didFinishPicking 委托中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM