与 SDWebImage 一起使用时,UICollectionViewCell 保持黑色
UICollectionViewCell remain black when used with SDWebImage
我想从 Instagram API 获取图像,然后使用 SDWebImage
在我的 iOS 应用程序的 UICollectionView
中显示它们。但是在我将图像加载到单元格后,单元格仍然是黑色的。
这是 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)
但是,单元格在屏幕上保持黑色。
我也是调用cell.contentView.backgroundColor = UIColor.redColor()
查了一下,然后就被红屏了。但是画面还是没有显示在屏幕上。
如何在单元格上显示图像?
更新
我尝试使用相同的 addSubview:
将 UILabel
添加到它的单元格中,标签显示正确。我不确定为什么图像不是这种情况...
您的 UIImageView 似乎没有框架。试试这个:
let imageView = UIImageView(frame: cell.contentView.frame.bounds)
我想从 Instagram API 获取图像,然后使用 SDWebImage
在我的 iOS 应用程序的 UICollectionView
中显示它们。但是在我将图像加载到单元格后,单元格仍然是黑色的。
这是 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)
但是,单元格在屏幕上保持黑色。
我也是调用cell.contentView.backgroundColor = UIColor.redColor()
查了一下,然后就被红屏了。但是画面还是没有显示在屏幕上。
如何在单元格上显示图像?
更新
我尝试使用相同的 addSubview:
将 UILabel
添加到它的单元格中,标签显示正确。我不确定为什么图像不是这种情况...
您的 UIImageView 似乎没有框架。试试这个:
let imageView = UIImageView(frame: cell.contentView.frame.bounds)