在CollectionView中使用SDWebImage的疯狂内存问题(在tableviewCell内)

问题描述 投票:0回答:1

一旦我开始使用由SDWebImage填充的imageviews滚动集合视图,我无法弄清楚为什么我的应用程序的内存会报警。如果它们尚未下载,那么暂时没问题。但是一旦每个图像被下载,如果我甚至触摸滚动,应用程序ui变得没有响应并且内存火箭如此之高(超过1GB),不仅应用程序崩溃,整个操作系统崩溃并重新启动。

我有一个带有几个单元格的tableview,其中一个有一个我可以滚动的集合视图。我有一个数据对象,用于填充collectionViewCell中的所有标签和图像。 cellForItem非常复杂(很多if语句和switch语句)所以我尝试从那里移出一些代码并将其移动到我在collectionViewCell中设置的属性的didSet方法来保存数据对象。然后我将数据对象设置为itemForRow中的属性(在后台线程中),该属性触发单个collectionViewCell的didSet方法以填充标签并使用sd_set_image方法设置图像。这是tableViewCell上的相关代码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print(indexPath.row)
if itemStyle == .Grid {

  let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.resourceCellIdentifier, for: indexPath) as! ResourceCollectionViewCell
  cell.delegate = self
  cell.tag = indexPath.row

  if self.filter == Filter.Collections ||  self.resourceArray?[indexPath.row].actionContent == nil {
    let resource: ActionContentTemplate?
    if self.filter == Filter.Collections {
      resource = self.collectionResourceArray?[indexPath.row].actionContentTemplate
    } else {
      resource = self.resourceArray?[indexPath.row].actionContentTemplate
    }

    DispatchQueue.global(qos: .userInitiated).async {

      cell.dataObject = resource
    }
    return cell

  } else {.....

这是CollectionViewCell本身的didSet方法:

var dataObject: ActionContentTemplate? {
didSet {
  var cellRow: Int = 0

  if self.actionContentTemplate != nil {
    ....

    DispatchQueue.main.async {
      cellRow = self.tag

       .....

      switch self.actionContentTemplate?.contentTypeID?.uintValue {
      case ContentTypeVideo.rawValue,
           ContentTypeVideoRecording.rawValue,
           ContentTypeScreenRecording.rawValue:
        .......
      case ContentTypeAttachment.rawValue:


        let fileType = (self.actionContentTemplate?.contentFileName as NSString?)?.pathExtension.lowercased()
        var placeholderImage: UIImage

        if fileType == "pdf" {
          placeholderImage = UIImage(named: "pdf")!
        } else if fileType == "doc" {
          placeholderImage = UIImage(named: "doc")!
        } else if fileType == "docx" {
          placeholderImage = UIImage(named: "doc")!
        } else if fileType == "xls" {
          placeholderImage = UIImage(named: "xls")!
        } else if fileType == "xlsx" {
          placeholderImage = UIImage(named: "xls")!
        } else if fileType == "ppt" {
          placeholderImage = UIImage(named: "ppt")!
        } else if fileType == "pptx" {
          placeholderImage = UIImage(named: "ppt")!
        } else {
          placeholderImage = UIImage(named: "plain")!
        }

        if let urlString = self.actionContentTemplate?.thumbnailURL {
          let imageURL = URL(string: urlString)

          SDWebImageManager.shared().loadImage(with: imageURL, options: [.continueInBackground, .scaleDownLargeImages], progress: nil) { (image, data, err, cacheType, finished, url) in
              DispatchQueue.main.async {
                if cellRow == self.tag {

                self.activityIndicator?.stopAnimating()
                self.imageView.image = image
                UIAnimation.fadeTransitionImageView(self.imageView)
              }
            }
          }
                      }

        } else {
          DispatchQueue.main.async {

            self.activityIndicator?.stopAnimating()
            self.imageView.image = placeholderImage
          }
        }
        break
.....

我实际上切换到loadImage方法,因为sd_setimage方法也没有工作。

 SDWebImageManager.shared().loadImage(with: imageURL, options: [.continueInBackground, .scaleDownLargeImages], progress: nil) { (image, data, err, cacheType, finished, url) in
                  DispatchQueue.main.async {
                    if cellRow == self.tag {
//IF I COMMENT OUT THESE TWO LINES BELOW THE ISSUES STOP (I think it is only the self.imageView.image = image line that is causing issues)
         //           self.activityIndicator?.stopAnimating()
         //           self.imageView.image = image
                    UIAnimation.fadeTransitionImageView(self.imageView)
                  }
                }
              }

任何人都可以向我解释为什么会发生这种情况以及我可以采取哪些措施来解决这个问题?

编辑 - 忘记在app delegate中添加这些设置以提问:

//Settings for image caching
  [[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:NO];

  [[[SDImageCache sharedImageCache] config] setShouldDecompressImages:NO];
  [[[SDImageCache sharedImageCache] config] setShouldCacheImagesInMemory:NO];
  [[[SDImageCache sharedImageCache] config] setMaxCacheSize:1024 * 1024 * 100];
  [[[SDImageCache sharedImageCache] config] setMaxCacheAge:3600 * 24 * 7];
  [[SDImageCache sharedImageCache] setMaxMemoryCost:1024 * 1024 * 15];
ios swift out-of-memory sdwebimage image-caching
1个回答
0
投票

原来缩略图相当大,并使用此链接(iOS Swift: How to properly scale down an image?)将图像大小调整为imageView大小完全修复了内存问题。然而,在下载图像之后,滚动有点不稳定....我想将尽可能多的计算代码移动到后台线程并且从cellForItem中移出会有所帮助,但我显然缺少一些东西。如果有人能指出我在正确的方向,我将不胜感激,因为我总是试图作为开发人员学习更多。

© www.soinside.com 2019 - 2024. All rights reserved.