title: How to select multiple images from UICollectionView and transfer them to another View Controller?
body: <p>The code is written in Swift. I'm building a social app where the user can make posts. I'm using Firebase as a backend (database, storage). So, I have a <code>UICollectionView</code> that gets all the photos from the photo library of the device and populate the collection view using a custom cell. In the same View controller, I have another custom cell that the user can use to take a photo and use it to make a post. To make it clearer:  </p>
  
  <ul>
  <li><p>If the user decides to take a photo, when they click on "Use photo" they need to be presented to a new view controller that should display the photo they just took along with other options (such as title, description and tags using <code>UITextFields</code> &amp; <code>UITextView</code>). </p></li>
  <li><p>If the user decides to select multiple photos from their own library, I have to somehow mark those photos/cells (i.e. using a button for a checkmark), add the selected photos to an array (with some limit, maybe 10 photos top). When they click "Next" button, the array needs to be sent to the New Post View Controller where all the images should be dynamically displayed maybe using a horizontal <code>UICollectionView</code> (?!) (with an option to remove an image if it was selected by accident) and again, as above, have the opportunity to add title, description, etc. Now, I cannot figure out how to do any of that.</p></li>
  </ul>
  
  <p>I looked for a solution, but I'm kind of stuck on this for couple days now, so help is very much welcome!</p>
  
  <p><strong>Here's what I have in the Collection View controller</strong> (PS: I didn't include the part with the function that gets the images from the Photos) </p>
  
  <pre><code>import UIKit
  import Photos
  
  class PrePhotoPostVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UICollectionViewDelegateFlowLayout {
  
  @IBOutlet weak var nextButton: UIBarButtonItem!
  var photosLibraryArray = [UIImage]()
  
  @IBOutlet weak var collectionView: UICollectionView!
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      checkPhotoLibraryPermission()
      setupCollectionViewDelegates()
  }
  
  @IBAction func cancelButtonPressed (_ sender: UIBarButtonItem) {
      dismiss(animated: true, completion: nil)
  }
  
  @IBAction func nextButtonPressed (_ sender: UIBarButtonItem) {
      nextButton.isEnabled = false
  }
  
  @IBAction func takeAphotoButtonPressed (_ sender: UIButton) {
  
      // Camera Autorization
      AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { response in
          if response {
              if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
                  let imagePicker = UIImagePickerController()
                  imagePicker.delegate = self
                  imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
                  imagePicker.allowsEditing = false
                  self.present(imagePicker, animated: true, completion: nil)
              }
              else {
                  print("Camera isn't available in similator")
              }
          }
          else {
              print("unautorized")
          }
      }
  }
  
   func numberOfSections(in collectionView: UICollectionView) -&gt; Int {
      return 2
  }
   func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      if section == 0 {
          return 1
      } else {
          return photosLibraryArray.count
      }
   }
  
   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
      if indexPath.section == 0 {
          let cellCamera = collectionView.dequeueReusableCell(withReuseIdentifier: cellPrePostCameraCell, for: indexPath) 
          return cellCamera
      }
      else {
          let cellPhotoLibrary = collectionView.dequeueReusableCell(withReuseIdentifier: cellPrePostPhotoLibrary, for: indexPath) as! PrePhotoPostPhotoLIbraryCell
          cellPhotoLibrary.awakeFromNib()
          cellPhotoLibrary.photoLibraryImage.image = photosLibraryArray[indexPath.row]
          return cellPhotoLibrary
      }
  }
  }
  </code></pre>
  
  <p>A screenshot of what this <code>UICollectionView</code> looks like: </p>
  
  <p><a href="https://i.stack.imgur.com/NzQug.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NzQug.png" alt="enter image description here"></a></p>
  
  <p><strong>Here's my code from the Photo Library Cell:</strong> </p>
  
  <pre><code>import UIKit
  
  class PrePhotoPostPhotoLIbraryCell: UICollectionViewCell {
  
      // MARK: Outlets
      @IBOutlet weak var photoLibraryImage: UIImageView!
  
      // var selectedPhotos = [UIImageView]()
  
      @IBAction func selectedButtonPressed(_ sender: UIButton) {
          self.layer.borderWidth = 3.0
          self.layer.borderColor = isSelected ? UIColor.blue.cgColor : UIColor.clear.cgColor
      }
      override func awakeFromNib() {
  
          photoLibraryImage.clipsToBounds = true
          photoLibraryImage.contentMode = .scaleAspectFill
          photoLibraryImage.layer.borderColor = UIColor.clear.cgColor
          photoLibraryImage.layer.borderWidth = 1
          photoLibraryImage.layer.cornerRadius = 5
  
      }
  }
  </code></pre>