1

I want to be able to allow users to upload image from photo library to a specific cell in collectionview when it is selected. I have two problems here.

(1) I am not sure if I call the cellTapped() function correctly.

(2) I want to be able to use tag to store indexpath.row but not sure how tag can be called in imagepickercontroller function. Help is appreciated.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let cell: UICollectionViewCell = collectionView.cellForItem(at: indexPath)!
    cell.layer.borderWidth = 4.0
    cell.layer.borderColor = UIColor.blue.cgColor
    cellTapped()

}


func cellTapped(){
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .photoLibrary
    imagePicker.allowsEditing = false
    present(imagePicker, animated: true, completion: nil)
}


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


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    guard let selectedImage = info[.originalImage] as? UIImage else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
    }
    dismiss(animated: true, completion: nil)
    collectionView?.reloadData()
}
Rengers
  • 14,911
  • 1
  • 36
  • 54
Ricky Geng
  • 177
  • 1
  • 2
  • 10
  • By seeing code I feel that you can simply pass the cell object to the cellTapped method from the didselect method. Once you have the cell object available you can assign the image to it after it gets selected uiimagepicker. Is this that you want? Let me know if you need some other solution. – tendstoZero Nov 21 '18 at 05:03
  • @tendstoZero Thanks. I think that's what I am looking for. But I am not sure how to write the code. Help from you would be appreciated. – Ricky Geng Nov 21 '18 at 05:16

1 Answers1

0

You need to remember which cell you tapped before picking image. You can save the index in collectionView(_:, didSelectItemAt:)

// instance property
private var selectedCellIndexPath: IndexPath?

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    selectedCellIndexPath = indexPath
    let cell: UICollectionViewCell = collectionView.cellForItem(at: indexPath)!
    cell.layer.borderWidth = 4.0
    cell.layer.borderColor = UIColor.blue.cgColor
    cellTapped()

}

Then you need to update you data source when the image is picked and reload collection.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    guard let selectedImage = info[.originalImage] as? UIImage else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
    }
    dismiss(animated: true, completion: nil)

    // let us assume that your collection relies on some array called 'images'
    guard let selectedCellIndexPath = selectedCellIndexPath,
          selectedCellIndexPath.item < images.count else {
          return
    }

    images[selectedCellIndexPath.item] = images
    collectionView?.reloadData()
}
Yevgeniy Leychenko
  • 1,287
  • 11
  • 25