我现在有一个UIImageView,其中加载了一个图像,其中的contentMode被设置为scaleAspectFill。是否有一种方法可以指定要显示的图像的确切部分,而不是默认的中心,还是我以错误的方式来处理这个问题?
Swift代码:
basketballImage = UIImageView()
basketballImage.contentMode = .scaleAspectFill
basketballImage.clipsToBounds = true
basketballImage.image = UIImage(named: "Basketball")
原始图像
期望结果
失败示例1:与contentMode = .scaleAspectFill
失败示例2:与contentMode = .top
发布于 2019-01-09 22:24:51
iOS不可能知道您真正想要的是什么,因为您的缩放是任意的。
通过设置contentScaleFactor
属性,可以有效地裁剪(放大)。体裁与你的目标无关。
现在,任意垂直偏移量需要仿射转换,但只能使用容器UIView
来维护布局。您必须在父框架内转换整个UIImageView
帧(除非您想要非常花哨地重写draw
方法或使用仿射转换钩子重新实现UIImageView )。
通过安装2倍分辨率的映像并使用以下方法,我大致实现了您想要的结果:
override func viewDidLoad() {
super.viewDidLoad()
let offset: CGFloat = 120.0
let scale: CGFloat = 3.0
/* imageView is a *child* of imageContainer and constrained to be exactly the same size and position */
imageContainer.clipsToBounds = true
imageView.image = UIImage(named: "basketball")
imageView.contentMode = .center
imageView.contentScaleFactor = scale
imageView.transform = CGAffineTransform(translationX: 0, y: offset)
}
这比我想的要难,因为有另一层嵌套,但是UIImageView
没有那么复杂。
https://stackoverflow.com/questions/54118649
复制相似问题