繁体   English   中英

collectionView图片:在Swift中动态更改高度

[英]collectionView Images: Change Height Dynamically in Swift

在这种情况下,图像正在下载并显示到collectionview ,并且工作正常,但是图像以不同的方式显示。 我已经显示了所有图像,它们都具有相同的高度和宽度。 如果图像较小,则它将采用默认的图像高度和宽度。 我已经显示所有图像都正确显示了相同的高度和宽度。

在此处输入图片说明

   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而不是拉伸它。

不过,如果您希望在显示器中使用相同的尺寸,请尝试将scaleAspectFill contentMode更改为scaleAspectFillscaleToFill

cell?.storeImageView.contentMode = UIViewContentMode.scaleAspectFill

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

您还可以选择另一种方式,会让你的用户界面更好看-设置backgroundColorimageView并将其设置为contentModescaleAspectFit

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM