Search code examples
iosswiftuicollectionviewsdwebimage

UICollectionViewCell remain black when used with SDWebImage


I want to fetch images from Instagram API and then show them in my iOS app's UICollectionView using SDWebImage. But after I load the image to cells the cells remain black.

Here's the code within cellForItemAtIndexPath::

let photo = self.photosArray[indexPath.row] as! AnyObject
let photoUrlString = photo.valueForKeyPath("images.standard_resolution.url")! as! String
print(photoUrlString) // properly show the URL 
let imageView = UIImageView()
imageView.sd_setImageWithURL(NSURL(string: photoUrlString), placeholderImage: nil)
print(cell.frame) // confirmed that the frame is not 0
cell.contentView.addSubview(imageView)

However, the cell remain black on the screen.

I also check it by calling cell.contentView.backgroundColor = UIColor.redColor(), then it is wrapped in red screen. But the image was still not displayed on the screen.

How can I display the image on the cell?

UPDATE

I tried adding UILabel to its cell using the same addSubview:, the label was displayed correctly. I'm not sure why this is not the case with the image...


Solution

  • It seems like your UIImageView does not have a frame. Try this:

    let imageView = UIImageView(frame: cell.contentView.frame.bounds)