请不要标记为重复,我没有找到考虑可用线程的解决方案.
我有一个UIImageView,用户可以下载UIImages各种格式.问题是,我需要UIImageView根据给定的Image比例调整大小.
目前我正在使用Aspect fit,但其中UIImageView很大一部分仍然是空的.我希望UIImageView根据其内容调整大小.例如,如果图片是1:1,4:3,6:2,16:9 ......
非常感谢帮助.
根据要求,这就是我想要的:
我有一个UIImageView正方形,装有16:7的图像或其他什么,并UIImageView调整大小以适应图像的大小...
ole*_*234 41
我花了很多时间试图找到你遇到的同样问题的解决方案,这是唯一对我有用的解决方案(Swift 4,xCode 9.2):
class ScaledHeightImageView: UIImageView {
override var intrinsicContentSize: CGSize {
if let myImage = self.image {
let myImageWidth = myImage.size.width
let myImageHeight = myImage.size.height
let myViewWidth = self.frame.size.width
let ratio = myViewWidth/myImageWidth
let scaledHeight = myImageHeight * ratio
return CGSize(width: myViewWidth, height: scaledHeight)
}
return CGSize(width: -1.0, height: -1.0)
}
}
Run Code Online (Sandbox Code Playgroud)
将类添加到项目中,并将UIImageView设置为自定义类ScaledHeightImageView.图像视图的内容模式是Aspect Fit.
我的问题与本文所述的问题相同.在我的原型TableViewCell的ContentView中,我有一个垂直StackView约束到每个边缘.在StackView内部有一个Label,ImageView和另一个Label.将ImageView设置为AspectFit是不够的.图像将是适当的大小和比例,但ImageView没有包裹实际图像,在图像和标签之间留下了一堆额外的空间(就像上图中一样).ImageView高度似乎与原始图像的高度相匹配,而不是调整大小的图像的高度(在aspectFit完成它的工作之后).我找到的其他解决方案由于各种原因并未完全解决问题.我希望这可以帮助别人.
Yun*_*HEN 28
看起来你想根据图像比例和容器视图的大小来调整ImageView的大小,这里是Swift中的例子(对不起,以前的答案有一个bug,我修复了它):
let containerView = UIView(frame: CGRect(x:0,y:0,width:320,height:500))
let imageView = UIImageView()
if let image = UIImage(named: "a_image") {
let ratio = image.size.width / image.size.height
if containerView.frame.width > containerView.frame.height {
let newHeight = containerView.frame.width / ratio
imageView.frame.size = CGSize(width: containerView.frame.width, height: newHeight)
}
else{
let newWidth = containerView.frame.height * ratio
imageView.frame.size = CGSize(width: newWidth, height: containerView.frame.height)
}
}
Run Code Online (Sandbox Code Playgroud)
Rai*_*idi 27
我花了很多时间在这上面,我终于得到了一个适合我的解决方案(Swift 3):
constraintHeight)然后,当我需要显示图像时,我只需编写以下内容(从上面的答案中采样):
let ratio = image.size.width / image.size.height
let newHeight = myImageView.frame.width / ratio
constraintHeight.constant = newHeight
view.layoutIfNeeded()
Run Code Online (Sandbox Code Playgroud)
基本上,这可以确保图像填充UIImageView的宽度并强制UIImageView的高度在缩放后等于图像的高度
Ank*_*hah 12
将 ImageView 放置在 ViewController 中并在viewDidLoad()name中创建一个出口imageView。
override func viewDidLoad() {
super.viewDidLoad()
var image = UIImage(contentsOfFile: "yourFilePath")!
var aspectR: CGFloat = 0.0
aspectR = image.size.width/image.size.height
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = image
imageView.contentMode = .scaleAspectFit
NSLayoutConstraint.activate([
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
imageView.leadingAnchor.constraint(greaterThanOrEqualTo: view.leadingAnchor),
imageView.trailingAnchor.constraint(lessThanOrEqualTo: view.trailingAnchor),
imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: 1/aspectR)
])
}
Run Code Online (Sandbox Code Playgroud)
数组的最后 3 行NSLayoutConstraint.activate确保图像宽度保持在容器视图的边界内,并且高度保持与宽度成比例(即保持纵横比,并将高度imageView缩小到所需的最小值)。
Interface Builder 中的视图控制器:Main.storyboard
正在运行的应用程序中 UIImageView 的快照:appSnapshot
| 归档时间: |
|
| 查看次数: |
54607 次 |
| 最近记录: |