title: Deleting Multiple Images in a Collection View
body: <p>How would I delete an image from my collection view of images? Secondly, how would I select multiple images and either bring up the view with sharing options or be able to delete them from the collection view? I am relatively new to programming and if someone has a solution or knows of a better way to do this, please respond, as it would be greatly beneficial. Thank you.</p>
  
  <pre><code>class ViewController: UICollectionViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDelegateFlowLayout {
  
  
  @IBOutlet weak var imageViewTwo: UIImageView!
  
      var imageArray = [UIImage]()
  
  override func viewDidLoad() {
  super.viewDidLoad()
  imageViewTwo.isHidden = true
  
  }
  
  
  
  @IBAction func chooseImage(_ sender: UIBarButtonItem) {
  
  let imagePickerController = UIImagePickerController()
  imagePickerController.delegate = self
  
  let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a Source", preferredStyle: .actionSheet)
  
  actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action: UIAlertAction) in
  
  if UIImagePickerController.isSourceTypeAvailable(.camera) {
  imagePickerController.sourceType = .camera
  self.present(imagePickerController, animated: true, completion: nil)
  } else {
  print("Camera is not available.")
  }
  
  }))
  
  actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action: UIAlertAction) in
  imagePickerController.sourceType = .photoLibrary
  self.present(imagePickerController, animated: true, completion: nil)
  }))
  
  actionSheet.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
  
  self.present(actionSheet, animated: true, completion: nil)
  
  }
  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
  
      imageViewTwo.image = image
      imageArray.append(imageViewTwo.image!)
      collectionView.reloadData()
  
  
  picker.dismiss(animated: true, completion: nil)
  }
  
  func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  picker.dismiss(animated: true, completion: nil)
      }
  
  
  
  
  
  override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      return imageArray.count
  }
  override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
  
      let imageView = cell.viewWithTag(1) as! UIImageView
      imageView.image = imageArray[indexPath.row]
  
  
  
  
      return cell
  
  
  }
  
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
  
      let width = collectionView.frame.width / 3 - 1
  
      return CGSize(width: width, height: width)
  }
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -&gt; CGFloat {
      return 1.0
  }
  
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -&gt; CGFloat {
      return 1.0
      }
  
  var itemsSelected = [IndexPath]()
  
  override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
          collectionView.allowsMultipleSelection = true
      if !(itemsSelected.contains(indexPath)) {
          itemsSelected.append(indexPath)
      }
  }
  @IBAction func deletePressed(_ sender: UIBarButtonItem) {
  
      itemsSelected.forEach {
          imageArray.remove(at:$0.row)
      }
      collectionView.deleteItems(at:itemsSelected)
      itemsSelected.removeAll()
  }
  
  
  
  }
  </code></pre>