Ios 如何以编程方式在TableViewCell内设置fix ImageView大小?

Ios 如何以编程方式在TableViewCell内设置fix ImageView大小?,ios,swift,uitableview,uiimageview,Ios,Swift,Uitableview,Uiimageview,我有一个问题,我需要固定tableviewcell内图像的大小。下图显示图像大小不均匀 这是我使用的代码 if noteImageIsAvailable == true { if let imageData = assignedNotePhoto?.photoData { if let image = Utilities.resizePictureImage(UIImage(data: imageData as Data)) {

我有一个问题,我需要固定tableviewcell内图像的大小。下图显示图像大小不均匀

这是我使用的代码

 if noteImageIsAvailable == true {
        if let imageData = assignedNotePhoto?.photoData {
            if let image = Utilities.resizePictureImage(UIImage(data: imageData as Data)) {
                //added fix
                cell.imageView?.frame.size = CGSize(width: 36, height: 24)
                cell.imageView?.clipsToBounds = true
                //-----
                cell.imageView?.contentMode = .scaleAspectFill
                cell.imageView?.image = image
            }
        }
    }
我在stackoverflow上看到了答案。它说我需要添加
clipToBounds
,但不幸的是它不起作用。请帮我解决这个问题。多谢各位

表格视图代码

extension SettingNoteViewController: UITableViewDataSource, UITableViewDelegate {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return Menu.SettingNote.items.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "reuseIdentifier")
    let keyPass = KeyHelper.NoteSetting.info[indexPath.row]
    let assignedNotePhoto = self.getNotePhoto(key: keyPass.key)
    let assignedNoteTextData = self.getNoteTextData(key: keyPass.key)?.value

    cell.contentView.backgroundColor = .black
    cell.textLabel?.textColor = .white
    cell.detailTextLabel?.textColor = .white
    cell.detailTextLabel?.numberOfLines = 0
    let noteImageIsAvailable = assignedNotePhoto?.photoData != nil

    if noteImageIsAvailable == true {
        if let imageData = assignedNotePhoto?.photoData {
            if let image = Utilities.resizePictureImage(UIImage(data: imageData as Data)) {
                //added fix
                cell.imageView?.translatesAutoresizingMaskIntoConstraints = false
                cell.imageView?.frame.size = CGSize(width: 36, height: 24)
                cell.imageView?.clipsToBounds = true
                //-----
                cell.imageView?.contentMode = UIView.ContentMode.scaleAspectFit
                cell.imageView?.image = image
            }
        }
    }
    cell.textLabel?.text = Menu.SettingNote.items[indexPath.row].value
    cell.detailTextLabel?.text = assignedNoteTextData ?? "noteSettingSubTitle".localized

    return cell
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.destination is InputMemoViewController {
        let vc = segue.destination as! InputMemoViewController
        vc.infoNoteKey = self.infoNoteKeyToPass
        vc.infoLabelText = self.infoLabelToPass
    }
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.infoNoteKeyToPass = KeyHelper.NoteSetting.info[indexPath.row].key
    self.infoLabelToPass = KeyHelper.NoteSetting.info[indexPath.row].label
    self.performSegue(withIdentifier: "showInputMemo", sender: self)
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
   return 80
   }
}
下图是我应用@Kishan Bhatiya solution时的输出。

第一和第二个图像是风景照片,第三个图像是肖像
我也有同样的问题,
scaleAspectFit
为我解决了。你可以试试

cell.imageView.contentMode = UIViewContentMode.scaleAspectFit

当您添加
cell.imageView?.translatesAutoresizingMaskIntoConstraints=false
时,则
frame
无效,因此请尝试使用任何一种

    let marginguide = contentView.layoutMarginsGuide


    //imageView auto layout constraints

    cell.imageView?.translatesAutoresizingMaskIntoConstraints = false

    let marginguide = cell.contentView.layoutMarginsGuide

    cell.imageView?.topAnchor.constraint(equalTo: marginguide.topAnchor).isActive = true
    cell.imageView?.leadingAnchor.constraint(equalTo: marginguide.leadingAnchor).isActive = true
    cell.imageView?.heightAnchor.constraint(equalToConstant: 40).isActive = true
    cell.imageView?.widthAnchor.constraint(equalToConstant: 40).isActive = true

    cell.imageView?.contentMode = .scaleAspectFill
    cell.imageView?.layer.cornerRadius = 20 //half of your width or height

最好在
UITableViewCell

中设置约束,因为您知道自己的图像视图大小。因此,您可以将UIImage调整为imageView大小

extension UIImage{
    func resizeImageWithHeight(newW: CGFloat, newH: CGFloat) -> UIImage? {
        UIGraphicsBeginImageContext(CGSize(width: newW, height: newH))
        self.draw(in: CGRect(x: 0, y: 0, width: newW, height: newH))
        
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return newImage
    }
}
像那样使用

let img = originalImage.resizeImageWithHeight(newW: 40, newH: 40)
cell.imageView?.image = img

这里,40 40是我的imageview大小

如何添加
单元格.imageview?.TranslatesAutoResizezingMaskintoConstraints=false
到您的代码中?显示您的tableView代码。您是否创建了UITableViewCell?@nwyyy您好,我也尝试过添加它,但仍然没有work@HabinLama您好,我为
TableView
添加了代码,我没有在我的故事板中添加
tableviewcell
删除
cell.imageView?.translatesAutoresizingMaskIntoConstraints=false
并进行检查,因为如果启用了
AutoLayout
,则
frame
无效,或者您可以检查我的回答谢谢您的帮助。我尝试使用您的解决方案修复它,但它会影响
标题
副标题
的布局。请检查上面添加的图像。这是我使用您的解决方案时的输出。感谢您将
Title
SubTitle
的前导锚点设置为单元格的
imageView
尾随锚点,如果您使用
AutoLayout
设置了约束,对不起,怎么做最好在
UITableViewCell
中创建这些约束,谢谢您的帮助。现在可以了。我只需要修复分隔符,因为当图像为横向时,它与
标题
副标题
不对齐。非常感谢你D