固定cell.imageView.image的大小

本文介绍了一种简单的方法来调整UITableViewCell中的图片视图大小,确保图片能够根据单元格的高度进行缩放,同时保持图片的长宽比不变。这种方法适用于不同尺寸的图片,并提供了代码示例。

cell.imageView.image的大小 会随着Cell的高度而变化,不同的图片显示的也不一样,在网上找了几种方法,简单方便的是下面这种:

UIImage *icon = [UIImage imageWithName:@"xxxxx"];
CGSize itemSize = CGSizeMake(30, 30);
UIGraphicsBeginImageContextWithOptions(itemSize,NO,0.0);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[icon drawInRect:imageRect];
 
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

另外还有其它方法没有成功:设置cell.imageView.contentMode的属性,
还有一种是网上转载的比较多的一种,由于不是自定义的cell,没用到此方法 :

重定义一个uitableviewcell,它自动生成的代码不需要动,只要在.m文件中再加上下面的代码即可:

(具体可参看:http://blog.youkuaiyun.com/zhangjie1989/article/details/7550990)

- (void)layoutSubviews {

    [superlayoutSubviews];

    self.imageView.bounds =CGRectMake(0,0,44,44);

    self.imageView.frame =CGRectMake(0,0,44,44);

    self.imageView.contentMode =UIViewContentModeScaleAspectFit;

    

    CGRect tmpFrame = self.textLabel.frame;

    tmpFrame.origin.x = 46;

    self.textLabel.frame = tmpFrame;

    

    tmpFrame = self.detailTextLabel.frame;

    tmpFrame.origin.x = 46;

    self.detailTextLabel.frame = tmpFrame;    

}

// // OnlineDeviceListViewController2.swift // SurveillanceHome // // Created by MaCong on 2025/10/13. // Copyright © 2025 tplink. All rights reserved. // import UIKit class OnlineDeviceListViewController2: RefreshableDeviceListViewController { private let reuseIdentifier = "deviceCell" var deviceOnlineStatus: TPSSVMSGetDevicePageStatus = .all // collectionView的布局对象 private lazy var collectionViewLayout: UICollectionViewFlowLayout = { let layout = UICollectionViewFlowLayout() layout.scrollDirection = .vertical layout.minimumInteritemSpacing = 10 layout.minimumLineSpacing = 10 return layout }() // collectionView private lazy var deviceCollectionView: UICollectionView = { let view = UICollectionView(frame: .zero, collectionViewLayout: collectionViewLayout) view.backgroundColor = .white view.register(DeviceCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) return view }() override func viewDidLoad() { super.viewDidLoad() setupView() // 设置 dataSource 和 delegate deviceCollectionView.dataSource = self deviceCollectionView.delegate = self } override var collectionView: UICollectionView { return deviceCollectionView } private func setupView() { // 设置collectionView约束 deviceCollectionView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(deviceCollectionView) NSLayoutConstraint.activate([ deviceCollectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), deviceCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor), deviceCollectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor), deviceCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor) ]) } } // MARK: - UICollectionViewDataSource extension OnlineDeviceListViewController2: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 4 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! DeviceCollectionViewCell cell.imageView.image = UIImage(named: "devicelist_default_cover_nvr_new") return cell } } // MARK: - UICollectionViewDelegateFlowLayout extension OnlineDeviceListViewController2: UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let width = (collectionView.frame.width - 30) / 2 // 每列宽度 return CGSize(width: width, height: 150) // 高度固定为150 } } // MARK: - 自定义cell class DeviceCollectionViewCell: UICollectionViewCell { let imageView: UIImageView = { let imageView = UIImageView() imageView.contentMode = .scaleAspectFit imageView.translatesAutoresizingMaskIntoConstraints = false return imageView }() override init(frame: CGRect) { super.init(frame: frame) contentView.addSubview(imageView) NSLayoutConstraint.activate([ imageView.topAnchor.constraint(equalTo: contentView.topAnchor), imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), imageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor) ]) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } 给这四张图片分别增加设备名称
最新发布
10-14
你目前的代码中 `DeviceCollectionViewCell` 没有添加 `UILabel` 来显示设备名称,也没有在 `cellForItemAt` 方法中设置对应的名称。下面我将为你完整地修改代码,**给四张图片分别增加设备名称**。 --- ### ✅ 修改说明: 1. **在 `DeviceCollectionViewCell` 中添加一个 UILabel 用于显示设备名称** 2. **在 `collectionView(_:cellForItemAt:)` 方法中,根据 indexPath 设置不同的图片和名称** --- ### ✅ 完整修改后的代码如下: ```swift // MARK: - 自定义cell(已添加 UILabel) class DeviceCollectionViewCell: UICollectionViewCell { let imageView: UIImageView = { let imageView = UIImageView() imageView.contentMode = .scaleAspectFit imageView.translatesAutoresizingMaskIntoConstraints = false return imageView }() let nameLabel: UILabel = { let label = UILabel() label.textAlignment = .center label.font = UIFont.systemFont(ofSize: 14) label.textColor = .black label.translatesAutoresizingMaskIntoConstraints = false return label }() override init(frame: CGRect) { super.init(frame: frame) contentView.addSubview(imageView) contentView.addSubview(nameLabel) NSLayoutConstraint.activate([ imageView.topAnchor.constraint(equalTo: contentView.topAnchor), imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), imageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), imageView.bottomAnchor.constraint(equalTo: nameLabel.topAnchor, constant: -5), nameLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), nameLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), nameLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor) ]) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } ``` ```swift // MARK: - UICollectionViewDataSource extension OnlineDeviceListViewController2: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 4 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! DeviceCollectionViewCell // 设置图片名称 let imageNames = ["1", "2", "3", "4"] if indexPath.row < imageNames.count { cell.imageView.image = UIImage(named: imageNames[indexPath.row]) } else { cell.imageView.image = UIImage(named: "devicelist_default_cover_nvr_new") } // 设置设备名称 let deviceNames = ["Camera A", "NVR-01", "Door Sensor", "Smart Light"] if indexPath.row < deviceNames.count { cell.nameLabel.text = deviceNames[indexPath.row] } else { cell.nameLabel.text = "Unknown" } return cell } } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值