ios 如何在 Swift 3 中根据 UIImage 的大小/比例调整 UIImageView 的大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41154784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resize UIImageView based on UIImage's size/ratio in Swift 3?
提问by David Seek
I have a UIImageView
and the user is able to download UIImages
in various formats. The issue is that I need the UIImageView
to resize based on the given Image's ratio.
我有一个UIImageView
,用户可以下载UIImages
各种格式。问题是我需要UIImageView
根据给定的图像比例调整大小。
Currently, I'm using Aspect fit
, but the UIImageView
remains empty on big parts of itself. I would like to have the UIImageView
resize itself based on its content. E.g if the pic is 1:1, 4:3, 6:2, 16:9...
目前,我正在使用Aspect fit
,但UIImageView
它本身的大部分仍然是空的。我想UIImageView
根据其内容调整大小。例如,如果图片是 1:1、4:3、6:2、16:9...
Help is very appreciated.
非常感谢帮助。
As requested, that is what I want:
根据要求,这就是我想要的:
I have had an UIImageView
that was square, loaded with an Image in 16:7 or whatever, and the UIImageView
resized to fit the size of the Image...
我有一个UIImageView
方形的,加载了 16:7 或其他UIImageView
格式的图像,并且调整了大小以适应图像的大小......
采纳答案by Yun CHEN
It looks like you want to resize an ImageView according to the image ratio and the container view's size, here is the example in Swift (Sorry,the former answer with a bug, I fixed it):
看起来您想根据图像比例和容器视图的大小调整 ImageView 的大小,这是 Swift 中的示例(对不起,前一个答案有错误,我已修复):
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)
}
}
回答by olearyj234
I spent many hours trying to find a solution to the same problem you're having and this is the only solution that worked for me (Swift 4, xCode 9.2):
我花了很多时间试图找到解决您遇到的相同问题的方法,这是唯一对我有用的解决方案(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)
}
}
Add the class to the project and set the UIImageView to the custom class ScaledHeightImageView. The image view's content mode is Aspect Fit.
将该类添加到项目中,并将 UIImageView 设置为自定义类 ScaledHeightImageView。图像视图的内容模式是 Aspect Fit。
My problemis the same as the one stated in this post. Inside my prototype TableViewCell's ContentView, I have a vertical StackView constrained to each edge. Inside the StackView there was a Label, ImageView and another Label. Having the ImageView set to AspectFit was not enough. The image would be the proper size and proportions but the ImageView didn't wrap the actual image leaving a bunch of extra space between the image and label (just like in the image above). The ImageView height seemed to match height of the original image rather than the height of the resized image (after aspectFit did it's job). Other solutions I found didn't completely resolve the problem for various reasons. I hope this helps someone.
我的问题和这篇文章中提到的一样。在我的原型 TableViewCell 的 ContentView 中,我有一个垂直 StackView 约束到每个边缘。在 StackView 里面有一个 Label、ImageView 和另一个 Label。将 ImageView 设置为 AspectFit 是不够的。图像将是正确的大小和比例,但 ImageView 没有包裹实际图像,在图像和标签之间留下了一堆额外的空间(就像上图一样)。ImageView 高度似乎与原始图像的高度相匹配,而不是调整后图像的高度(在 aspectFit 完成它的工作之后)。由于各种原因,我发现的其他解决方案并没有完全解决问题。我希望这可以帮助别人。
回答by Merricat
I spent many hours on this, and I finally got a solution that worked for me (Swift 3):
我花了很多时间在这上面,我终于得到了一个对我有用的解决方案(Swift 3):
- in IB, I set UIImageView's 'Content Mode' to 'Aspect Fit'
- in IB, I set UIImageView's width constraintto be equal to whatever you want (in my case, the view's width)
- in IB, I set UIImageView's height constraintto be equal to 0, and create a referencing outletfor it (say,
constraintHeight
)
- 在 IB 中,我将 UIImageView 的“内容模式”设置为“Aspect Fit”
- 在 IB 中,我将 UIImageView 的宽度约束设置为等于您想要的任何值(在我的情况下,视图的宽度)
- 在 IB 中,我将 UIImageView 的高度约束设置为等于 0,并为其创建一个引用出口(例如,
constraintHeight
)
Then, when I need to display the image, I simply write the following (sampled from answers above):
然后,当我需要显示图像时,我只需编写以下内容(从上面的答案中取样):
let ratio = image.size.width / image.size.height
let newHeight = myImageView.frame.width / ratio
constraintHeight.constant = newHeight
view.layoutIfNeeded()
Basically, this ensures that the image fills the UIImageView's width and forces the UIImageView's height to be equal to the image's height after it scaled
基本上,这可以确保图像填充 UIImageView 的宽度并强制 UIImageView 的高度在缩放后等于图像的高度
回答by John Bushnell
The solution I used is based on olearyj234's solution, but makes having no image take up essentially no space (or more specifically the minimum iOS will accept). It also uses ceil to avoid problems which can occur with non-integer values when UIImageView's are embedded in things like scrolling cells.
我使用的解决方案是基于olearyj234 的解决方案,但使没有图像占用基本上没有空间(或更具体地说,最小的 iOS 将接受)。它还使用 ceil 来避免当 UIImageView 嵌入在滚动单元格等内容中时非整数值可能出现的问题。
class FixedWidthAspectFitImageView: UIImageView
{
override var intrinsicContentSize: CGSize
{
// VALIDATE ELSE RETURN
// frameSizeWidth
let frameSizeWidth = self.frame.size.width
// image
// ? In testing on iOS 12.1.4 heights of 1.0 and 0.5 were respected, but 0.1 and 0.0 led intrinsicContentSize to be ignored.
guard let image = self.image else
{
return CGSize(width: frameSizeWidth, height: 1.0)
}
// MAIN
let returnHeight = ceil(image.size.height * (frameSizeWidth / image.size.width))
return CGSize(width: frameSizeWidth, height: returnHeight)
}
}
回答by user8969729
The solution is also based on olearyj234's solution, but I think this will help more people.
该解决方案也是基于olearyj234的解决方案,但我认为这会帮助更多人。
@IBDesignable
class DynamicImageView: UIImageView {
@IBInspectable var fixedWidth: CGFloat = 0 {
didSet {
invalidateIntrinsicContentSize()
}
}
@IBInspectable var fixedHeight: CGFloat = 0 {
didSet {
invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
var size = CGSize.zero
if fixedWidth > 0 && fixedHeight > 0 { // 宽高固定
size.width = fixedWidth
size.height = fixedHeight
} else if fixedWidth <= 0 && fixedHeight > 0 { // 固定高度动态宽度
size.height = fixedHeight
if let image = self.image {
let ratio = fixedHeight / image.size.height
size.width = image.size.width * ratio
}
} else if fixedWidth > 0 && fixedHeight <= 0 { // 固定宽度动态高度
size.width = fixedWidth
if let image = self.image {
let ratio = fixedWidth / image.size.width
size.height = image.size.height * ratio
}
} else { // 动态宽高
size = image?.size ?? .zero
}
return size
}
}
回答by Ben Ong
Set your imageView to aspectFit, that will resize the image to not exceed your imageView's frame.
将您的 imageView 设置为 aspectFit,这将调整图像大小以不超过您的 imageView 的框架。
You can get the size of your UIImage of your imageView with logic from this question- basically just get the height and width of the UIImage.
您可以使用this question的逻辑获取imageView的UIImage的大小-基本上只需获取UIImage的高度和宽度。
Calculate the ratio and set the width/height of the imageView to fit you screen.
计算比例并设置 imageView 的宽度/高度以适合您的屏幕。
There is also a similar questionto your that you might get you answer from.