Search code examples
iosswift2uicollectionviewsdwebimage

Swift2 iOS- SDWebImage Flickering in UICollectionView


I have an array of image strings that I'm pulling from Firebase. I convert those images to an array of NSURLs then load those urls into my collectionView using sdwebimage. I get the images but I keep getting constant flickering of the images. Where am I having the issue at?

@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
    }

Solution

  • You are using sd_setAnimationImagesWithURLs in your cellForItemAtIndexPath

    If you don't want that then use sd_setImageWithURL and you need to pass single URL at a time for your item, like this

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