CollectionView Cell Image: Replace placeholder image with a camera photo.

Hi,

I have an UICollecitonView that loads a placeholder image within a ColletionView cell without any problems. The placeholder image is loaded via ‘Assets’ using a string property.

class SampleImageView: UIImageView {

let placeholderImage = UIImage(named: "Sample")

override init(frame: CGRect) {
super.init(frame: frame)
configure()

}
''''

func setImage(holder: UIImage) {
image = holder
}

private func configure() {
layer.cornerRadius = 10
clipsToBounds = true
image = placeholderImage
translatesAutoresizingMaskIntoConstraints = false
}

}


In addition, I would like to replace the placeholder image with the one taking via the camera. I know that the image from the camera is loading without any any problems.


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

guard let image = info[.editedImage] as? UIImage else {
print("No image found")
return
}
imageTaken = true
placeHolderImage.contentMode = .center
placeHolderImage.image = resizeImage(image: image, targetSize: CGSize(width: 44, height: 44))
}

And then from my CollectionView VC:

extension SampleCollectionVC: SampleMoveDataDelegate {
func didTapAdd(data: SampleData) {

// Verify that the VC is getting the image passed to it.
let photoSize = data.sampleImage?.size.width
print(photoSize)
Code Block
// Append data then retake
if LoadData.shared.getPerscriptionData().isEmpty {
LoadData.shared.update(perscription: medicaiton)
configureViewController()
configureCollectionView()
configureDataSource()
updateData()
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}


The collecitonView loads fine, but the placeholder image is displayed and not the camera image. Also fYI, I am applying the datasource snapshot on the main thread. And finally, here's the data struct:

struct SampleData: Hashable {
var sampleName: String // Perscription name
var sampleImage: UIImage?
}


Does anyone have any recommendations to handle this situation?

Thanks,
Answered by pdl5000 in 625161022
The issue was that I was not properly setting the image in the CollectionView Cell. Once I correct this error, the camera image displayed with out any issues.
Accepted Answer
The issue was that I was not properly setting the image in the CollectionView Cell. Once I correct this error, the camera image displayed with out any issues.
CollectionView Cell Image: Replace placeholder image with a camera photo.
 
 
Q