ios - 如何在 Swift 中设置 tableview 上的 ImageView 高度?

标签 ios swift tableview

enter image

带有图像选择器的Tableview,当选择tableview中的任何单元格显示图像选择器 View 时,当我从选择器中选择图像时,该图像显示到tableview中的确切单元格中,所有工作正常,但图像显示不同的布局,高度我有精确高度显示图像

   func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            let path = IndexPath(row: self.selectedCell.row, section: 0)
            let cell = Tableview.cellForRow(at: path) as! CustomTableviewCell
            cell.documentImage?.image  = pickedImage
            cell.documentImage?.frame = CGRect()
        }
        Tableview.reloadData()
        picker.dismiss(animated: true, completion: nil)
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableviewCell
        if SegmentController.selectedSegmentIndex == 0
        {
            cell.prooofLable?.text = idProof[indexPath.row]
        }else  if SegmentController.selectedSegmentIndex == 1
        {
            cell.prooofLable?.text = addressProof[indexPath.row]
            
        }
        return cell
    }

最佳答案

swift 4

在storyoard中将ImageView的图像内容模式更改为aspectfitaspectfill,并勾选要绑定(bind)的剪辑。

您也可以通过编程来执行此操作

cell.documentImage?.clipsToBounds = true
cell.documentImage?.contentMode = .scaleAspectFit

这是您的方法中的完整代码。

   func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            let path = IndexPath(row: self.selectedCell.row, section: 0)
            let cell = Tableview.cellForRow(at: path) as! CustomTableviewCell
            cell.documentImage?.image  = pickedImage
            cell.documentImage?.clipsToBounds = true //clips out the image out of the cell
            cell.documentImage?.contentMode = .scaleAspectFit //fits the streacth view in image
            cell.documentImage?.frame = CGRect()
        }
        Tableview.reloadData()
        picker.dismiss(animated: true, completion: nil)
    }

关于ios - 如何在 Swift 中设置 tableview 上的 ImageView 高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56649357/

相关文章:

ios - 为什么 Swift 同时具有 NSDictionary 和 Dictionary 类?

ios - swift 中的单元格 ImageView cornerRadius

ios - 后退按钮后推送不起作用?

ios - 从网络服务器(ASIHTTPRequest、PHP、mysql)更新 iOS TableView 单元格中的数据

iphone - 更改 UITabBarItem 中的字体

ios - 使 UISlider 高度变大?

ios - 自动闭包的函数是异步的吗?

Swift 5.5 异步操作作为 optional 默认值

ios - 无法调用非函数类型“UITableView”的值

ios - 如何将 subview 添加到 TableViewCell?