【发布时间】: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