Search code examples
iosasynchronousuitableviewsdwebimage

Why using SDWebImage in a UITableViewCell triggers ImageViews not to render properly?


I have a UITableViewController containing my own cells that I dequeue in cellForRowAtIndexPath. After dequeuing, I configure the cell and reload, asynchronously, the image for that cell.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  if let cell = tableView.dequeueReusableCellWithIdentifier("PeopleCell", forIndexPath: indexPath) as? PeopleListViewCell {
    cell.configureCell(headImageUrl)
  }
}

In my PeopleListView class,

func configureCell(img:NSURL) {
  if headImageView == nil { // to avoid allocation memory if not used
    headImageView = UIImageView()
    addSubView(headImageView)
  }
  headImageView.sd_cancelCurrentImageLoad()
  headImageView.sd_setImageWithURL(headImageUrl)
}

It works fine at the first loading and also while scrolling.

But when I push another viewController after

didSelectRowAtIndexPath

and come back to the list after the

dismissViewController()

I end up with a weird effect on my UIImageView , it's kind of a stacked or ghost image effect.. I'm having a hard time to figure out where is even triggered as when I m coming back from the viewController, cellForRowAtIndexPath is not called.


Solution

  • This actually has nothing to do with asynchronous image loading. The images I had was displayed within circles, with a cornerRadius. Somehow, it was displayed without any problem at first load...

    The issue here is I simply forgot the

    headImageView.layer.maskToBounds = true
    

    The result I got before setting maskToBounds to true was that I had the feeling that multiple images were located within the headImageView (UIImageView). If you ever have some artefacts like that, I hope this question/answer will help you.