无法在 UITableViewCell 中缩放图像

Can't scale image in UITableViewCell

我有一个小的 UITableViewCell 和一个巨大的 UIImage(如图所示)。

我必须将这张图片作为我手机的背景。我想缩放图像以使其宽度等于单元格宽度。然后我想从这个图像中显示一小块(这块应该从中间裁剪),这块的高度应该等于我的 UITableViewCell 的高度。 我尝试使用我的单元格背景视图的 scaleMode,但没有任何积极的事情发生(你能帮我做吗?

您应该检查 UIImageViewcontentMode 属性。 UIViewContentModeScaleAspectFill 根据 Apple docs 看起来很适合你:

UIViewContentModeScaleAspectFill :

The option to scale the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds.

Available in iOS 2.0 and later.

试试这个:

将 UIImage 固定到 TableViewCell 内容的所有 4 个边缘。然后选择 UIImage 将其视图模式设置为 Aspect Fill。我相信这会将图像覆盖到单元格的宽度并从中心点裁剪。