带有图像选择器的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的图像内容模式更改为aspectfit
或aspectfill
,并勾选要绑定(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/