修改cell.imageView的大小

本文介绍如何使用SwiftUI调整UIImageView中的图片大小,并设置图片的圆角和阴影效果等样式属性。通过创建合适尺寸的上下文并利用UIGraphicsGetImageFromCurrentImageContext获取新的图片,可以有效地调整图片大小。

cell.imageView?.sd_setImage(with: NSURL.init(string: imageStr ) as URL!, placeholderImage: UIImage.init(named: "usericon.png"))

//改变图片大小

        let itemSize:CGSize = CGSize.init(width: 50, height: 50)

        UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.main.scale)

        let rect11 :CGRect = CGRect.init(x: 0, y: 0, width: itemSize.width, height: itemSize.height)

        cell.imageView?.image?.draw(in: rect11)

        cell.imageView?.image = UIGraphicsGetImageFromCurrentImageContext()

        cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 13)

        cell.selectionStyle = .none

        cell.imageView?.layer.masksToBounds = true

        cell.imageView?.layer.cornerRadius = 25

import UIKit class OnlineDeviceListViewController2: OnlineDeviceListViewController { // 使用自定义属性名,不覆盖父类的collectionView private var customCollectionView: UICollectionView? // 单张图片数据 private let image: UIImage = { if #available(iOS 13.0, *) { return UIImage(named: "device_evaluation_fiveStars") ?? UIImage(systemName: "photo")! } else { return UIImage(named: "device_evaluation_fiveStars") ?? UIImage(named: "defaultImage") ?? UIImage() } }() override func viewDidLoad() { super.viewDidLoad() setupCustomCollectionView() // 使用自定义方法名避免冲突 } private func setupCustomCollectionView() { let layout = UICollectionViewFlowLayout() layout.itemSize = CGSize(width: view.bounds.width - 40, height: view.bounds.width - 40) layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20) // 初始化自定义collectionView customCollectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) guard let collectionView = customCollectionView else { return } collectionView.backgroundColor = .white collectionView.register(ImageCell.self, forCellWithReuseIdentifier: "ImageCell") collectionView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(collectionView) NSLayoutConstraint.activate([ collectionView.topAnchor.constraint(equalTo: view.topAnchor), collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor), collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor), collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor) ]) collectionView.dataSource = self collectionView.delegate = self } } // MARK: - 数据源协议 extension OnlineDeviceListViewController2: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell cell.configure(with: image) return cell } } // MARK: - 代理协议 class ImageCell: UICollectionViewCell { private let imageView = UIImageView() override init(frame: CGRect) { super.init(frame: frame) setupUI() } required init?(coder: NSCoder) { fatalError("init(coder:)未实现") } private func setupUI() { backgroundColor = .lightGray layer.cornerRadius = 8 clipsToBounds = true imageView.contentMode = .scaleAspectFit imageView.translatesAutoresizingMaskIntoConstraints = false contentView.addSubview(imageView) NSLayoutConstraint.activate([ imageView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor), imageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor), imageView.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.9), imageView.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: 0.9) ]) } func configure(with image: UIImage) { imageView.image = image } } 将这个改为两个并列显示的图片,两个cell并排显示,用相同的图片即可
最新发布
10-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值