如何将图像放入收藏夹视图中?

问题描述 投票:1回答:2

目前,我有以下图像:enter image description here

我想在容器中插入图片。但是当我运行这段代码时

//MARK: - UICollectionViewDataSource protocol

// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.items.count
}
//make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    //get  a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! AppIconViewCell

    // Use the outlet in our custom class to get a reference to the UILabel in the cell
    cell.myLabel.text = self.items[indexPath.item]
    cell.ImageView.image = UIImage.init(named: "page-01")

    //cell.backgroundColor = UIColor.cyan
    cell.layer.borderColor = UIColor.black.cgColor
    cell.layer.borderWidth = 1
    cell.layer.cornerRadius = 8

    return cell
}

结果是这样的:enter image description here

我的理解是图像大于导致框架扩展的框架。

如何在内部设置图像,使其适合第一张图像中的框?

ios swift uicollectionview uicollectionviewcell
2个回答
0
投票

这是一个更简单的问题。因此,将来我建议您进行一些搜索,因为那样您会更快地学习基础知识。

           cell.ImageView.image = UIImage.init(named: "page-01")
            cell.ImageView.contentMode = .scaleAspectFill
            cell.clipsToBounds = true

这是基本清单。当您访问图像视图上的.contentMode属性时,您可能想尝试可用的选项,以查看最喜欢的选项。

https://developer.apple.com/documentation/uikit/uiview/1622619-contentmode


0
投票

欢迎使用Stackoverflow。

为此,您需要添加显式的heightwidth约束。我假设您有适当的大小可以在方法sizeForItemAt中返回。

最后,不要忘记将cellImageViewclipsToBounds设置为true

© www.soinside.com 2019 - 2024. All rights reserved.