-2

I have a collection view. I want to fill each cell with the images that are stored in the apps core data. However the cells only fill with the last image that was taken. not each individual cell benign filled with each individual image. my code is as follows:

  1. when the user takes a photo it is compressed and stored in core data:

    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
       }
       // print out the image size as a test
       print(image.size)
    
       //convert image to savable type for coredata
       var jpegImageData = image.jpegData(compressionQuality: 1.0)
       jegImage = jpegImageData!
    
       //calling function in coredataManger to save image in data format
       CoreDataManager.sharedInstance.addImage()
    

    }

  2. The adding to core data function that was activated above:

    func addImage() {
    let entityName =  NSEntityDescription.entity(forEntityName: "ImageData", in: context)
    let image2 = NSManagedObject(entity: entityName!, insertInto: context)
    image2.setValue(jegImage, forKeyPath: "imageName")
    do {
      try context.save()
        print("image is saved")
    } catch let error as NSError {
      print("Could not save. \(error), \(error.userInfo)")
    }
    }
    
  3. ive global variables to pass images from one VC to another

     //global variables for images saved to core data
     var jegImage = Data()
     var theImagePulled = UIImage()
    
  4. Trying to populate the collection view cell with the images stored in core data:

     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeRoomCell", for: indexPath) as! HomeRoomCell
         let name = cartoonArr[indexPath.item]
    
         //setting image shown in the colleciton view equal to the gloabl images stored in core data
         let image = theImagePulled
         cell.bindData(name: name, image: image)
         return cell
     }
    

So the problem is that the cell gets populated but they all populate with the same image. this image is the last image that was added to core data model, even though there are several images in the core data model. I have been playing around with trying to set up an array that stores all these core data images but i can't figure it out. any ideas?

kitchen800
  • 197
  • 1
  • 12
  • 36

1 Answers1

0

Get rid of the global variables, which are indeed capturing the most recent image.

Use NSFetchedResultsController in your collection view.

If you’re new to Core Data, you might consider passing just a string around and displaying that, first. Then move on to images.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42