gpt4 book ai didi

ios - collectionView 图片 : Change Height Dynamically in Swift

转载 作者:行者123 更新时间:2023-11-28 11:33:33 25 4
gpt4 key购买 nike

在该场景中,图像正在下载并显示到 collectionview 中,它工作正常,但图像以不同的方式显示。我已经展示了所有的图像,它们都具有相同的高度和宽度。如果图像很小,那么它将采用默认的图像高度和宽度。我已经显示所有图像的高度和宽度都相同,并且显示正确。

enter image description here

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

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "storeCell", for: indexPath) as? EShoppingStoreCollectionViewCell
cell?.layer.borderColor = UIColor.groupTableViewBackground.cgColor
cell?.layer.borderWidth = 0.5

let urlData = storeDictionary?[indexPath.item]["logo"] as? String
cell?.storeImageView.contentMode = UIViewContentMode.scaleAspectFit
cell?.storeImageView.clipsToBounds = true
let frame = CGRect(x: (cell?.storeImageView.frame.width)!/2 - 30, y: (cell?.storeImageView.frame.height)! - 30, width: 60, height: 60)

if urlData != nil {
let url = URL(string: urlData!)
cell?.storeImageView.sd_setImage(with: url!, placeholderImage: UIImage(named:""), options: [SDWebImageOptions.continueInBackground, SDWebImageOptions.lowPriority, SDWebImageOptions.refreshCached, SDWebImageOptions.handleCookies, SDWebImageOptions.retryFailed]) { (image, error, cacheType, url) in
if error != nil {
print("Failed: \(String(describing: error))")
} else {
print("Success")
}
}
}
return cell!
}

如何设置所有图片大小相同

最佳答案

image 的大小不在你手中。它是从 API 接收到的内容。因此,最好使用分辨率正确的图像并将 contentMode 设置为 scaleAspectFit 而不是拉伸(stretch)它。

不过,如果您想要相同的显示尺寸,请尝试将 imageView 的 contentMode 更改为 scaleAspectFillscaleToFill

cell?.storeImageView.contentMode = UIViewContentMode.scaleAspectFill

这肯定会拉伸(stretch)图像,但您在显示中看到的所有图像都将覆盖 imageView 的大小。

您还可以选择另一种方式,让您的 UI 看起来更好 - 设置 imageViewbackgroundColor 并将其 contentMode 设置为 scaleAspectFit

关于ios - collectionView 图片 : Change Height Dynamically in Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56237472/

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