Home > Net >  How to select Three images with three buttons in Swift
How to select Three images with three buttons in Swift

Time:10-18

On my screen I need to add three images for that I have three separate buttons

code: here EasyImagePicker is my custom imagepicker.. but with my below code if I select any one image from imagepicker then it's adding in all three images

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
        }
    }
}

how to select three different images for three images using imagepicker.. please do guide me

CodePudding user response:

The reason is there is no thing here that can differentiate which button is clicked , hence result set to all imageviews

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
    }
} 

You need

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
                    }
        }
                 
    }
}

CodePudding user response:

Add picker reference to func didSelect(image: UIImage?, video: URL?, fileName: String?)

This way func didSelect(picker: EaseImagePicker, image: UIImage?, video: URL?, fileName: String?)

From picker call it with the reference to self 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
    }
}
}
  • Related