gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:55:52 25 4
gpt4 key购买 nike

我正在使用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这个链接来解决它,但是没有任何改变。

有什么想法吗?

最佳答案

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

imageView.translatesAutoresizingMaskIntoConstraints = false

以编程方式使用自动约束时,应使用此选项

关于ios - 如何使用SDWebImage调整下载图像的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42690865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com