Swift2 iOS- UICollectionView 中的 SDWebImage 闪烁

Swift2 iOS- SDWebImage Flickering in UICollectionView

我有一组从 Firebase 中提取的图像字符串。我将这些图像转换为 NSURL 数组,然后使用 sdwebimage 将这些 url 加载到我的 collectionView 中。我得到了图像,但图像不断闪烁。我在哪里遇到问题?

@IBOutlet weak var collectionView: UICollectionView!

//There are a maybe 2 or 3 strings inside here
var imageStrings = [...]()
var imageNSURLs:[NSURL] = []


override func viewDidLoad() {
        super.viewDidLoad()
        for imageString in self.imageStrings{
            let url = NSURL(string: imageString)
            self.imageNSURLs.append(url)
        }

        self.collectionView.reloadData()
    }

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

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

        //This is the sdwebimage method I use: sd_setAnimationImagesWithURLs(arrayOfURLs: [AnyObject]!)
        cell.imageView.sd_setAnimationImagesWithURLs(self.imageNSURLs)

        return cell
    }

您正在 cellForItemAtIndexPath

中使用 sd_setAnimationImagesWithURLs

如果您不想那样,请使用 sd_setImageWithURL 并且您需要一次为您的项目传递单个 URL,就像这样

cell.imageView.sd_setImageWithURL(imageNSURLs[indexPath.row])