如何在CollectionView中的Cell中更改imageView(SubView)的大小

时间:2017-03-29 10:35:15

标签: ios swift

我的单元格图像有问题。更准确地说,资产中的图像比细胞更大。我如何将图像转换为更小的尺寸?

`var photos = [" IMG_3814"," IMG_3803"]

override func viewDidLoad() {
    super.viewDidLoad()


    // Do any additional setup after loading the view.
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return photos.count
}


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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath)
    let img = photos[indexPath.row]
    let imageView: UIImageView = UIImageView(image: UIImage(named: img))


    cell.contentView.addSubview(imageView)

    return cell
}`

2 个答案:

答案 0 :(得分:1)

尝试使用

let imageView: UIImageView = UIImageView(image: UIImage(named: img))
imageView.frame = cell.contentView.frame
imageView.contentMode = .scaleAspectFit

它会将您的图像调整为imageView

答案 1 :(得分:0)

试试这个

 let img = UIImage(named: photos[indexPath.row])
 let imageView: UIImageView = UIImageView(frame: cell.frame)
 imageView.image = img
 imageView.contentMode = .scaleAspectFit
 imageView.clipsToBounds = true
 cell.contentView.addSubview(imageView)
相关问题