【问题标题】:UICollectionViewCell Remote Image Disappears on ScrollUICollectionViewCell 远程图像在滚动时消失
【发布时间】:2018-04-11 01:16:27
【问题描述】:

我在视图控制器中有一个水平滚动 UICollectionView。每当滚动视图时,视图中的图像就会消失。我试图为重用做准备,但没有成功。我已经发布了下面的代码:

集合视图单元格

import UIKit
import SDWebImage

class StickerCell: UICollectionViewCell {
    @IBOutlet weak var stickerImage: UIImageView!

    override func prepareForReuse() {
        super.prepareForReuse()

        self.stickerImage.image = nil
    }

    func setImage(image: String) {
        let url = NSURL(string: image)
        self.stickerImage.contentMode = .ScaleAspectFit
        self.stickerImage.clipsToBounds = true
        self.stickerImage.center = self.center
        self.stickerImage.sd_setImageWithURL(url)
    }
}

视图控制器

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.stickers.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: StickerCell = collectionView.dequeueReusableCellWithReuseIdentifier("stickerCell", forIndexPath: indexPath) as! StickerCell

    let sticker = stickers[indexPath.row]

    if let image = sticker["images"]["medium"].string {
        cell.setImage(image)
    }

    return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    var cell = collectionView.cellForItemAtIndexPath(indexPath)
    if cell?.selected == true {
        let sticker = stickers[indexPath.row]
        if let image = sticker["images"]["medium"].string {
            let url = NSURL(string: image)
            self.stickerView.contentMode = .ScaleAspectFit
            self.stickerView.clipsToBounds = true
            self.stickerView.sd_setImageWithURL(url)
        }
    }
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSizeMake(115, 115)
}

【问题讨论】:

  • sd_setImageWithURL(url) 函数发生了什么?这是扩展程序吗?
  • 是的,它是 SD 网络图像。

标签: ios swift uicollectionview


【解决方案1】:

试试这个.. -> 从 strickerCell 类中删除 setImage 方法。 -> 实现这个

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: StickerCell = collectionView.dequeueReusableCellWithReuseIdentifier("stickerCell", forIndexPath: indexPath) as! StickerCell
let sticker = stickers[indexPath.row]

if let image = sticker["images"]["medium"].string {
     let url = NSURL(string: image)
    cell.stickerImage.contentMode = .ScaleAspectFit
    cell.stickerImage.clipsToBounds = true
   cell.stickerImage.sd_setImageWithURL(url, placeholderImage:placeHolderImage, completed: { (image, error, cacheType, url) -> Void in
   cell.stickerImage.image = image
    })
}

    return cell
}

【讨论】:

  • 这是我原来做的,导致了同样的问题。
【解决方案2】:

我用同样的方法系了这个,但图像似乎在一段时间或一段时间内消失了。尝试在 Sd_setImage 调用上设置一些选项:例如:

self.userProfile.sd_setImage(with: URL(string: self.senderData.userProfileimg), placeholderImage: #imageLiteral(resourceName: "blankProfile"), options: [SDWebImageOptions.progressiveDownload, SDWebImageOptions.continueInBackground], completed: { (image, error, CachedType, url) in
   self.userProfile.image = image
  })

【讨论】:

  • 试过了,但对我的情况不起作用..仍然不明白为什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-24
  • 2011-01-13
  • 1970-01-01
  • 2020-02-04
  • 1970-01-01
相关资源
最近更新 更多