UITableViewCell ImageView不能正确缩放?

我正在用用户select的图像填充UITableView 。 我希望表格单元格中的缩略图都是相同的大小,而不会影响宽高比,以免拉伸/倾斜图像,这对我来说就像ScaleAspectFill一样,但是没有一个UIViewContentModeselect似乎有效。 有我冲突的方法,着名的heightForRowAtIndexPath ,但删除这使我的细胞太小。 以下是我的didSelectRowAtIndexPathheightForRowAtIndexPath方法,以及从当前代码的模拟器(使用模拟器股票图像)的屏幕截图。 任何帮助表示赞赏。 从iOS模拟器截图

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //sets cell based on meme in array let cell = tableView.dequeueReusableCellWithIdentifier("memeCell") as! UITableViewCell let meme = self.memes[indexPath.row] // Set the name and image cell.textLabel?.text = meme.topText + " " + meme.bottomText cell.imageView?.frame = CGRectMake(0, 0, 200, 100) cell.imageView?.contentMode = UIViewContentMode.ScaleToFill //ScaleAspectFill is best, ScaleToFill used to exagerate that its not working //cell.imageView?.backgroundColor = UIColor.grayColor() //legacy code, will be removed at commit cell.imageView?.image = meme.origImage return cell } override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { //cell row won't be large enough unless this method is called return 75.0 } 

您可以添加UITableViewCell的子类,然后覆盖layoutSubviews方法:

 override func layoutSubviews() { super.layoutSubviews() self.imageView.frame = CGRect(0,0,200,100) } 

看看https://github.com/mattgemmell/MGImageUtilities

您可以在UIImage + ProportionalFill中使用裁剪function,按比例缩放图像以完全填充所需的大小,裁剪到中心,在分配图像之前将所有图像裁剪为相同的大小。

你可以像这样使用它:

 cell.imageView?.image = meme.origImage. imageCroppedToFitSize(CGSize(width: 200, height: 100))