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