我使用SDWebImage將圖像異步下載到UICollectionView單元中,但下載的圖像不適合單元格,單元格僅顯示圖像的上角。如何使用SDWebImage調整dowloaded圖像的大小?
這是我的代碼:
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絕對有效。 –