【问题标题】:how to resize dowloaded Image with SDWebImage?如何使用 SDWebImage 调整下载图像的大小?
【发布时间】:2025-11-22 09:45:01
【问题描述】:

我正在使用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 解决了它,但没有任何改变。

所以有什么想法吗?

【问题讨论】:

  • 嗨@Mohammad 接受的答案对你有用吗?
  • @nikdange_me 绝对有效。

标签: ios swift3 uicollectionview uicollectionviewcell sdwebimage


【解决方案1】:

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

imageView.translatesAutoresizingMaskIntoConstraints = false

当您以编程方式使用自动约束时应该使用它

【讨论】:

  • jazak allah kheer,(谢谢兄弟),请您解释一下。
  • 看看这些链接link,link我觉得够抓住重点了
  • 我很感激。
最近更新 更多