Add image in collectionView from gallery

Hi Developers, Need some solution for my problem.
I want to add more than 1 pictures In UICollectionView from Gallery . I had already done for only 1 picture in CollectionView(In a cell that has a UiImageView), But now I'm stuck that how to add multiple pictures in CollectionView from gallery.

Here is the my code for Only 1 picture

    @IBAction func chooseimage(_ sender: Any) {
      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
    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: .cancel, handler: nil))
    self.present(actionSheet, animated: true, completion: nil)

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell: photosCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "photosCollectionViewCell", for: indexPath) as! photosCollectionViewCell
    cell.cellImgView.image = selectedImage
    return cell

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 1

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage{
        selectedImage = image
        print("Something went wrong")
    picker.dismiss(animated: true, completion: nil)

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    picker.dismiss(animated: true, completion: nil)

Thanks in Advance.

Please ask questions about Apple-specific frameworks on the Apple Developer Forums.

Why this is Off-Topic?

This forum and this category is about pure Swift and not about how to use 3rd party modules or Apple-specific frameworks. We keep reminding people about that all the time. I highly encourage you to try the Apple Developer Forums or Stackoverflow for such questions. For convenience I'll start tagging such topics with [Off-Topic] so it will become more clear for new contributors. Thank you for your understanding. :)


The topic in here won't be deleted, so if someone decides to help you out, you'll see a response, but usually you have higher chances to get such questions answered on the other platforms. ;)


Right, Got your point.

1 Like


You can't do that with UIImagePickerController. Look into the Photos framework.

@DevAndArtist is of course right, but it's ok since you're new to the environment. I guess this once happened to all of us :)

The tagging is a smart idea. However maybe use a real tag instead so that people can actually filter them out?

Thanks :slight_smile:

I thought about that before doing it, but I decided not to apply tags because those are secondary meta-data which is more likely to be ignored by the majority. A [Tag] plied to the title is more notable and you can search it. The disadvantage is that you need to be aware about that [Tag].


Checked the profile settings. You're right is probably better to apply a real tag to filter out such topics from Latest.