preview page for uploaded image

Viewed 75

I am making a multiple images upload process; I want user to preview their chose images and add some description for images. In here, I make a collection view inside a table view, I want the images show in collection view cell, but I don’t know what I should code to call the images that user picked.

This is image picker controller

    @objc func btnClick() {
    let picker = YPImagePicker(configuration: config)
       
        picker.didFinishPicking { [unowned picker] items, _ in
            if let photo = items.singlePhoto {
                print(photo.fromCamera) // Image source (camera or library)
                print(photo.image) // Final image selected by the user
                print(photo.originalImage) // original image selected by the user, unfiltered
                print(photo.modifiedImage) // Transformed image, can be nil
                print(photo.exifMeta) // Print exif meta data of original image.
            }
            picker.dismiss(animated: true, completion: nil)
        }
        present(picker, animated: true, completion: nil)
        
        picker.didFinishPicking { [unowned picker] items, cancelled in
            for item in items {
                switch item {
                case .photo(let photo):
                    print(photo)
                case .video(let video):
                    print(video)
                }
            }
            picker.dismiss(animated: true, completion: nil)
        }

    }

performSegue(withIdentifier: "UploadTableViewController", sender: nil)

here is in the tableView controller

    func collectionView(collectionView: UICollectionView,
    cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("UploadCollectionViewCell",
        forIndexPath: indexPath)
    cell.image?.UIImageView = 
    return cell
}
2 Answers

I think you have to assign the image to your collection view cell in cellForItemAt:indexPath. So do something like:

cell.imageView.image =  items[indexPath.row]

What else is missing is that once you have picked an image you have to update the collectionView. So just reload it by calling:

collectionView.reloadData()

Hope it helps.

In CellForRowAt, it is necessary to set the corresponding Image in UIImageView of cell.

cell.image = imageData[indexPath.item]

imageData is a group of Image data that User can select.

Then reload collectionView at any time.

collectionView.reloadData()

example code.

func collectionView(collectionView: UICollectionView,
            cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
  let cell = collectionView.dequeueReusableCellWithReuseIdentifier("UploadCollectionViewCell",
    forIndexPath: indexPath)
  cell.image = imageData[indexPath.item]
  // It works with either item or row. But for CollectionView, .item is preferable.
  collectionView.reloadData()
  return cell
}
Related