如何 select 在 Swift 中使用三个按钮的三个图像

How to select Three images with three buttons in Swift

在我的屏幕上,我需要添加三个图像,因为我有三个单独的按钮

代码:这里 EasyImagePicker 是我的自定义图像选择器.. 但是如果我 select 来自图像选择器的任何一张图像然后使用我的下面的代码它会添加所有三个图像

class EditProfileImagesVC: UIViewController {
    
    private var imagePicker : EasyImagePicker?
    private var imagePicker1 : EasyImagePicker?
    private var imagePicker2 : EasyImagePicker?
    
    
    @IBOutlet weak var profilePicImage: UIImageView!
    @IBOutlet weak var leftProfilePicImg: UIImageView!
    @IBOutlet weak var rightProfilePicImg: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        imagePicker = EasyImagePicker(presentationController: self, delegate: self)
        imagePicker1 = EasyImagePicker(presentationController: self, delegate: self)
        imagePicker2 = EasyImagePicker(presentationController: self, delegate: self)
        
    }
    
    @IBAction func profilePicBtn(_ sender: UIButton) {
        imagePicker?.present(from: sender, mediaType: .images, onViewController: self)
    }
    
    @IBAction func leftProfileBtn(_ sender: UIButton) {
        imagePicker1?.present(from: sender, mediaType: .images, onViewController: self)
    }
    
    @IBAction func rightProfileBtn(_ sender: UIButton) {
        imagePicker2?.present(from: sender, mediaType: .images, onViewController: self)
    }
}

extension EditProfileImagesVC : EasyImagePickerDelegate{
    
    func didSelect(image: UIImage?, video: URL?, fileName: String?) {
        if let img = image{
            profilePicImage.image = img
        }
        if let img1 = image{
            leftProfilePicImg.image = img1
        }
        if let img2 = image{
            rightProfilePicImg.image = img2
        }
    }
}

如何使用 imagepicker select 三张不同的图片。请指导我

原因是这里没有东西可以区分点击了哪个按钮,因此结果设置为所有图像视图

func didSelect(image: UIImage?, video: URL?, fileName: String?) {
    if let img = image{
        profilePicImage.image = img
    }
    if let img1 = image{
        leftProfilePicImg.image = img1
    }
    if let img2 = image{
        rightProfilePicImg.image = img2
    }
} 

你需要

enum ImageChooser {
  case profile,left,right
}
class EditProfileImagesVC: UIViewController { 
    private var imagePicker : EasyImagePicker?
    @IBOutlet weak var profilePicImage: UIImageView!
    @IBOutlet weak var leftProfilePicImg: UIImageView!
    @IBOutlet weak var rightProfilePicImg: UIImageView!
    var chooser:ImageChooser = .profile
    override func viewDidLoad() {
        super.viewDidLoad()

        imagePicker = EasyImagePicker(presentationController: self, delegate: self)

    }

    @IBAction func profilePicBtn(_ sender: UIButton) {
        chooser = .profile
        imagePicker?.present(from: sender, mediaType: .images, onViewController: self)
    }

    @IBAction func leftProfileBtn(_ sender: UIButton) {
        chooser = .left
        imagePicker1?.present(from: sender, mediaType: .images, onViewController: self)
    }

    @IBAction func rightProfileBtn(_ sender: UIButton) {
        chooser = .right
        imagePicker2?.present(from: sender, mediaType: .images, onViewController: self)
    }
}


extension EditProfileImagesVC : EasyImagePickerDelegate{

    func didSelect(image: UIImage?, video: URL?, fileName: String?) {
        if let img = image {
            if chooser == .profile {
                profilePicImage.image = img
            }
            else
                if chooser == .left {
                    leftProfilePicImg.image = img
                }
                else
                    if chooser == .right {
                        rightProfilePicImg.image = img
                    }
        }
                 
    }
}

将选择器引用添加到 func didSelect(image: UIImage?, video: URL?, fileName: String?)

这样 func didSelect(picker: EaseImagePicker, image: UIImage?, video: URL?, fileName: String?)

从选择器调用它并引用自己 didSelect(picker: self, image: selectedImage, video: nil, fileName: nil)

func didSelect(picker: EaseImagePicker, image: UIImage?, video: URL?, fileName: String?) {
if let img = image{
    switch picker {
        case imagePicker:
            profilePicImage.image = img
        case imagePicker1:
            leftProfilePicImg.image = img
        case imagePicker2:
           rightProfilePicImg.image = img
        default:
            break
    }
}
}