如何使用SDWebImage调整下载的图像大小?

时间:2017-03-09 09:00:15

标签: ios swift3 uicollectionview uicollectionviewcell sdwebimage

我使用SDWebImage将图像异步下载到UICollectionView单元格中,但下载的图像并不适合单元格,单元格只显示图像的上角。

这是我的代码:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ImageCell

    cell.imageView.setShowActivityIndicator(true)
    cell.imageView.setIndicatorStyle(.gray)
    cell.imageView.sd_setImage(with: URL(string: imageURL[indexPath.row]))

    return cell
}

这是我的cellClass:

private class ImageCell: UICollectionViewCell {

override init(frame: CGRect) {
    super.init(frame: frame)
    self.setupViews()
}
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

let imageView: UIImageView = {
    let imageView = UIImageView()
    imageView.backgroundColor = .white
    imageView.layer.masksToBounds = true
    return imageView
}()

func setupViews() {
    backgroundColor = .white
    addSubview(imageView)
    imageView.contentMode = .scaleAspectFit
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.frame = CGRect(x: 0, y: 0, width: frame.width , height: frame.width )

}
}

我已按照此链接Resize UICollectionView cells after image inside has been downloaded来解决此问题,但没有任何变化。

那么任何想法?

1 个答案:

答案 0 :(得分:2)

如果您不想使用自动约束,则应删除此行

imageView.translatesAutoresizingMaskIntoConstraints = false

当您以编程方式使用自动约束时,您应该使用此功能

相关问题