简体   繁体   English

Swift调整大小uitablecell图像

[英]Swift resize uitablecell image

I just set my UITableCell linebreak to byWordWrapping and add this method: 我只是将UITableCell换行符设置为byWordWrapping并添加以下方法:

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

Now My UITableCell Images are massive, what is the best way to resize them or have the images aspect to fit? 现在,我的UITableCell图像很大,什么是调整它们的大小或使图像外观适合的最佳方法是什么?

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "commentsCell", for: indexPath)

        cell.textLabel?.numberOfLines = 0
        cell.textLabel?.lineBreakMode = .byWordWrapping

        cell.detailTextLabel?.numberOfLines = 0
        cell.detailTextLabel?.lineBreakMode = .byWordWrapping

        if((self.array[indexPath.row]["profileImg"] as! String) == "")
        {
            cell.textLabel?.text = (self.array[indexPath.row]["username"] as! String)
            cell.detailTextLabel?.text = (self.array[indexPath.row]["comment"] as! String)
            cell.imageView?.image = UIImage(named: "default.png")
        }
        else
        {
            let imgUrl = URL(string:"http://auxpod.com/uploads/" + (self.array[indexPath.row]["profileImg"] as! String))

            Alamofire.request(imgUrl!).responseImage { response in
                DispatchQueue.main.async {
                    if let image = response.result.value {
                        cell.textLabel?.text = (self.array[indexPath.row]["username"] as! String)
                        cell.detailTextLabel?.text = (self.array[indexPath.row]["comment"] as! String)
                        cell.imageView?.image = image
                    }
                }
            }
        }

        return cell

    }

1- 1-

Don't load the image inside cellForRowAt as it keeps download the image every scroll so use SDWebImage that caches it after first download 不要将图像加载到cellForRowAt内部,因为它会在每次滚动时都下载图像,因此请使用SDWebImage在第一次下载后将其缓存

2- 2-

Create a custom cell class , hook the the height constraint of the imageView and change it according to the required aspect ratio 创建一个自定义单元格类,挂钩imageView的高度约束,然后根据所需的宽高比进行更改

imageViewH.constant = imageRealHeight * imageCellWidth / imageRealWidth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM