iosiphoneswift4xcode9.3

Multiple Image uploads in One View Controller with different Buttons (iOS, Xcode9, Swift4)


Here have 4 UIImageView with seperate 4 buttons each for image upload. I couldn't fix when the user click each button appropriate image have to get uploaded. Mean when the user click back button, appropriate image have to be uploaded. I have attached my swift code, please help in this to fix

 var imagePicker = UIImagePickerController()
 var imagePicked = 0

@IBOutlet weak var adImage1: UIImageView!
@IBOutlet weak var adImage2: UIImageView!
@IBOutlet weak var adImage3: UIImageView!
@IBOutlet weak var adImage4: UIImageView!

 @IBAction func uploadImage1(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .photoLibrary;
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)
        }
    }

    @IBAction func uploadImage2(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            let imagePicker1 = UIImagePickerController()
            imagePicker1.delegate = self
            imagePicker1.sourceType = .photoLibrary;
            imagePicker1.allowsEditing = true
            self.present(imagePicker1, animated: true, completion: nil)
        }     }
    @IBAction func uploadImage3(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            let imagePicker2 = UIImagePickerController()
            imagePicker2.delegate = self
            imagePicker2.sourceType = .photoLibrary;
            imagePicker2.allowsEditing = true
            self.present(imagePicker2, animated: true, completion: nil)
        }  }
    @IBAction func uploadImage4(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            let imagePicker3 = UIImagePickerController()
            imagePicker3.delegate = self
            imagePicker3.sourceType = .photoLibrary;
            imagePicker3.allowsEditing = true
            self.present(imagePicker3, animated: true, completion: nil)
        }  }


     internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
     {
         let imagePicker = info[UIImagePickerControllerOriginalImage] as? UIImage
        if imagePicked == 1 {
       adImage1.image = imagePicker
        }
     else if imagePicked == 2
            {
                adImage2.image = imagePicker
            }
            else if imagePicked == 3

                {
                    adImage3.image = imagePicker
                }
              else if imagePicked == 4
                    {
                        adImage4.image = imagePicker
                 }                    else
                    {
                       // Error message
}
         self.dismiss(animated: true, completion: nil)
    }
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true)
    }

Solution

  • Change your button IBaction code to set image picked based on button click so that your code work like charm

    @IBAction func uploadImage1(_ sender: Any) {
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
                imagePicked = 1
                let imagePicker = UIImagePickerController()
                imagePicker.delegate = self
                imagePicker.sourceType = .photoLibrary;
                imagePicker.allowsEditing = true
                self.present(imagePicker, animated: true, completion: nil)
            }
        }
    
        @IBAction func uploadImage2(_ sender: Any) {
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
                imagePicked = 2
                let imagePicker1 = UIImagePickerController()
                imagePicker1.delegate = self
                imagePicker1.sourceType = .photoLibrary;
                imagePicker1.allowsEditing = true
                self.present(imagePicker1, animated: true, completion: nil)
            }
        }
        @IBAction func uploadImage3(_ sender: Any) {
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
                imagePicked = 3
                let imagePicker2 = UIImagePickerController()
                imagePicker2.delegate = self
                imagePicker2.sourceType = .photoLibrary;
                imagePicker2.allowsEditing = true
                self.present(imagePicker2, animated: true, completion: nil)
            }
        }
        @IBAction func uploadImage4(_ sender: Any) {
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
                imagePicked = 4
                let imagePicker3 = UIImagePickerController()
                imagePicker3.delegate = self
                imagePicker3.sourceType = .photoLibrary;
                imagePicker3.allowsEditing = true
                self.present(imagePicker3, animated: true, completion: nil)
            }
        }