0

I'm trying, as the title suggests, to give the possibility to change the photo once you've already selected one. At the moment the user cannot choose another photo if he does not deselect the previous one, so I am not interested in selecting multiple photos but only being able to move the cursor with each touch.

fileprivate func showImagePicker(sourceType: UIImagePickerControllerSourceType) {
        let imagePickerVC = UIImagePickerController()

        imagePickerVC.sourceType = sourceType
        imagePickerVC.delegate = self
        imagePickerVC.allowsEditing = true

        present(imagePickerVC, animated: true, completion: nil)
}
Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29
  • You can't , you have to create a custom view that shows media from photos in a collection view and apply multiple selection on those items – Abhinav Khanduja Oct 28 '19 at 10:00

1 Answers1

0

The UIImagePickerController doesn't allow for selecting multiple images. Check the top answer to this question: How to select Multiple images from UIImagePickerController - there is a long list of links for helpful external APIs and libraries. I'll choose the OpalImagePicker for this example. The OpalImagePicker has an option for setting the maximum limit of images a user can select, which means multiple images can be selected. You can make it 10 images, for example, by writing the line imagePicker.maximumSelectionsAllowed = 10. It is a wonderful API that allows you to customize the image picker to your control quite a bit. It has Swift 4 and Swift 5 compatibility. It even offers delegate methods to pick images from external sources like Facebook, Instagram, or Twitter.

To install, go to CocoaPods, and type in:

pod 'OpalImagePicker'

Sample code:

import OpalImagePicker

class DelegateExampleViewController: UIViewController {
    @IBAction func photoLibraryTapped(_ sender: UIButton) {
        let imagePicker = OpalImagePickerController()
        imagePicker.imagePickerDelegate = self
        imagePicker.selectionTintColor = UIColor.white.withAlphaComponent(0.7)
        imagePicker.selectionImageTintColor = UIColor.black
        imagePicker.selectionImage = UIImage(named: "x_image")  // Change image to X rather than checkmark
        imagePicker.statusBarPreference = UIStatusBarStyle.lightContent
        imagePicker.maximumSelectionsAllowed = 10  // This is the line that relates to your question
        let configuration = OpalImagePickerConfiguration()
        configuration.maximumSelectionsAllowedMessage = NSLocalizedString("You cannot select that many 
             images!", comment: "")
        imagePicker.configuration = configuration
        present(imagePicker, animated: true, completion: nil)
    }
}

extension DelegateExampleViewController: OpalImagePickerControllerDelegate {
    func imagePickerDidCancel(_ picker: OpalImagePickerController) {
        // Cancel action
    }

    func imagePicker(_ picker: OpalImagePickerController, didFinishPickingImages images: [UIImage]) {
        // Save Images, update UI   
        // Dismiss Controller
        presentedViewController?.dismiss(animated: true, completion: nil)
    }

    func imagePickerNumberOfExternalItems(_ picker: OpalImagePickerController) -> Int {
        return 1
    }

    func imagePickerTitleForExternalItems(_ picker: OpalImagePickerController) -> String {
        return NSLocalizedString("External", comment: "External (title for UISegmentedControl)")
    }

    func imagePicker(_ picker: OpalImagePickerController, imageURLforExternalItemAtIndex index: Int) -> URL? {
        return URL(string: "https://placeimg.com/500/500/nature")
    }
}
Max Voisard
  • 1,685
  • 1
  • 8
  • 18
  • As I said : so I am not interested in selecting multiple photos but only being able to move the cursor with each touch. – Vincenzo Flaminio Oct 28 '19 at 11:12
  • I'm a little confused about what you mean to be "able to move the cursor with each touch" – Max Voisard Oct 28 '19 at 15:57
  • When the user is on the library of the camera he can select a photo and a checkmark appear on that item, if he would like to change the item(photo) selected he can't . The only way is deselect the item chosen before and then select another item. – Vincenzo Flaminio Oct 29 '19 at 13:54