将核心数据图像添加到集合视图单元格

发布于 2025-01-20 18:57:06 字数 1719 浏览 3 评论 0原文

我有一个集合视图。我想用存储在应用程序核心数据中的图像填充每个单元格。然而,单元格仅填充最后拍摄的图像。 并不是每个良性的细胞都充满了每个单独的图像。 我的代码如下:

  1. 当用户拍照时,它被压缩并存储在核心数据中:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        picker.dismiss(动画:true)
    
       守护让图像=信息[.editedImage]为? UI图像其他{
           print("没有找到图片")
           返回
       }
       // 打印出图像大小作为测试
       打印(图像.尺寸)
    
       //将图像转换为coredata可保存的类型
       var jpegImageData = image.jpegData(压缩质量: 1.0)
       jegImage = jpegImageData!
    
       //调用coredataManger中的函数将图像以数据格式保存
       CoreDataManager.sharedInstance.addImage()
    

    }

  2. 上面激活的添加到核心数据功能:

    func addImage() {
    让entityName = NSEntityDescription.entity(forEntityName: "ImageData", in: context)
    让 image2 = NSManagedObject(实体: 实体名称!, insertInto: 上下文)
    image2.setValue(jegImage, forKeyPath: "imageName")
    做 {
      尝试 context.save()
        print("图像已保存")
    } catch let 错误为 NSError {
      print("无法保存。\(error), \(error.userInfo)")
    }
    }
    
  3. 使用全局变量将图像从一个 VC 传递到另一个 VC

     //保存到核心数据的图像的全局变量
     var jegImage = 数据()
     var theImagePulled = UIImage()
    
  4. 尝试使用存储在核心数据中的图像填充集合视图单元格:

     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
         让 cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeRoomCell", for: indexPath) as!家庭房间单元
         让 name = cartArr[indexPath.item]
    
         //设置集合视图中显示的图像等于核心数据中存储的全局图像
         让图像 = theImagePulled
         cell.bindData(名称:名称,图像:图像)
         返回单元格
     }
    

所以问题是单元格被填充,但它们都填充了相同的图像。该图像是添加到核心数据模型的最后一个图像,即使核心数据模型中有多个图像。我一直在尝试建立一个存储所有这些核心数据图像的数组,但我无法弄清楚。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初与友歌 2025-01-27 18:57:06

去掉全局变量,它们确实捕获了最新的图像。

在集合视图中使用 NSFetchedResultsController。

如果您是核心数据新手,您可能会考虑首先传递一个字符串并显示它。然后继续讨论图像。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文