简体   繁体   中英

Swift/XCode - Select ANOTHER using ImagePickerController

I am wondering how can I select multiple images using the ImagePickerController .

However, I don't wish to select multiple images within the ImagePickerController controller. Rather, my app is set where the user selects an imageView (they all have a tapGestureRecogniser ). There are 10. So whenever a user taps a certain imageView , I would like the image to be applied to the selected imageView .

However, it keeps overriding the first imageView .

How can I resolve this matter?

EDIT---

So imagine these are my imageViews:

[ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

I tap the third one and I select an image. The image should then be applied to the third imageView. And I can then decide to select the first imageView and add another image etc.

I hope this makes sense.

EDIT x 2 - My code:

            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleSelectedImage))
            selectedImage.addGestureRecognizer(tapGesture)
            selectedImage.isUserInteractionEnabled = true
     func handleSelectedImage() {

            let pickerController = UIImagePickerController()
            pickerController.delegate = self
            pickerController.mediaTypes = ["public.image", "public.movie"]
            pickerController.allowsEditing = true
            pickerController.modalPresentationStyle = .popover
            pickerController.popoverPresentationController?.delegate = self as! UIPopoverPresentationControllerDelegate
            pickerController.popoverPresentationController?.sourceView = selectedImage
            let alertController = UIAlertController(title: "Add a Picture", message: "Choose From", preferredStyle: .actionSheet)

            let cameraAction = UIAlertAction(title: "Camera", style: .default) { (action) in
                pickerController.sourceType = .camera
                self.present(pickerController, animated: true, completion: nil)

            }
            let photosLibraryAction = UIAlertAction(title: "Photos Library", style: .default) { (action) in
                pickerController.sourceType = .photoLibrary
                self.present(pickerController, animated: true, completion: nil)

            }

            let savedPhotosAction = UIAlertAction(title: "Saved Photos Album", style: .default) { (action) in
                pickerController.sourceType = .savedPhotosAlbum
                self.present(pickerController, animated: true, completion: nil)

            }

            let videoAction = UIAlertAction(title: "Videos", style: .default) { (action) in

                pickerController.mediaTypes = ["public.movie"]
                pickerController.sourceType = .photoLibrary
                self.present(pickerController, animated: true, completion: nil)

            }

            let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)

            alertController.addAction(cameraAction)
            alertController.addAction(photosLibraryAction)
            alertController.addAction(savedPhotosAction)
             alertController.addAction(videoAction)
            alertController.addAction(cancelAction)



            present(alertController, animated: true, completion: nil)

        }

        func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
            return .none

        }


extension AddPostViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIPopoverPresentationControllerDelegate {

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage {
            chosenImage = image
            selectedImage.image = image
            dismiss(animated: true, completion: {
                self.performSegue(withIdentifier: "returnHome", sender: nil)
            })
        }



    }

What you can do is use your imageView's tag property along with a variable to record which imageView made the call. Mentioned below is a rough outline.

  • Create a variable (say selectedImageView )
  • Set the tag property of your imageViews from 1-5
  • When an imageView makes a call to open the imagePickerController , set selectedImageView = imageView.tag
  • When an image is picked, check the value for selectedImageView and set the image to the imageView with the same tag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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