【问题标题】:How can I set image view height on tableview in Swift?如何在 Swift 中的 tableview 上设置图像视图高度?
【发布时间】:2019-11-01 02:42:09
【问题描述】:

带有 imagepicker 的 Tableview,当在 tableview 中选择任何单元格时显示图像选择器视图,当我从选择器中选择图像时,该图像显示到 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
    }

【问题讨论】:

  • 根据imageview的大小改变table view cell的高度
  • 将imageView的contentMode改为scaleAspectFit。

标签: ios swift tableview


【解决方案1】:
cell.documentImage?.clipsToBounds = true

【讨论】:

    【解决方案2】:

    斯威夫特 4

    将 Storyoard 中 ImageView 的图像 Contentmode 更改为 aspectfit 或 aspectfill 并勾选 Clips to bound。

    您也可以以编程方式执行此操作

    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)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多