如何在 UIImageView 中为图像应用宽高比后知道图像大小
我正在将图像加载到图像视图,模式为“Aspect Fit”。我需要知道图像缩放到的尺寸。请帮忙。
I am loading an image to an imageview with mode as 'Aspect Fit'. I need to know the size to which my image is being scaled to. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
这个简单的函数将计算纵横比适合后图像的大小:
Swift 5.1
Objective C:
This simple function will calculate size of image after aspect fit:
Swift 5.1
Objective C:
Swift 3 人类可读版本
Swift 3 Human Readable Version
我还想在应用纵横比后计算高度,以便能够计算表视图单元格的高度。所以,我通过一点数学实现
,高度将变成
所以代码片段将是
I also wanted to calculate height after the aspect ratio is applied to be able to calculate the height of table view's cell. So, I achieved via little math
and height would become
So code snippet would be
对于 Swift,请使用以下代码
并调用函数
For Swift use below code
And Call Function
也许不适合您的情况,但这种简单的方法解决了我在类似情况下的问题:
在图像视图执行 sizeToFit 后,如果您查询 imageView.frame.size 您将获得适合的新图像视图尺寸新的图像尺寸。
Maybe does not fit your case, but this simple approach solve my problem in a similar case:
After image view executes sizeToFit if you query the imageView.frame.size you will get the new image view size that fits the new image size.
斯威夫特4:
.aspectFit
图像的框架是 -let x: CGRect = AVMakeRect(aspectRatio: myImage.size, insideRect:sampleImageView.frame)
Swift 4:
Frame for
.aspectFit
image is -let x: CGRect = AVMakeRect(aspectRatio: myImage.size, insideRect: sampleImageView.frame)
Swift 3 UIImageView 扩展:
Swift 3 UIImageView extension:
这条线就可以完成这项工作
This single line can do this job
接受的答案非常复杂,并且对于某些边缘情况会失败。我认为这个解决方案更加优雅:
The accepted answer is incredibly complicated and fails for some edge cases. I think this solution is much more elegant:
这是我对同样问题的解决方案:
https://github.com/alexgarbarev/UIImageView-ImageFrame
优点:
Here is my solution for same problem:
https://github.com/alexgarbarev/UIImageView-ImageFrame
Advantages:
这是我的无 AVFoundation 解决方案。
首先,这是一个 CGSize 扩展,用于计算适合另一个尺寸的尺寸:
因此 OP 问题的解决方案可以归结为:
另外,这里还有另一个用于在另一个矩形中拟合矩形的扩展(它利用了上面的 CGSize 扩展):
Here's my AVFoundation-less solution.
First here's a CGSize extension for calculating a size that would fit another size:
So the solution to OP's problem gets down to:
Also here's another extension for fitting a rect within another rect (it utilizes the above CGSize extension):
Swift 5 扩展
Swift 5 Extension
我在 Swift 中使用以下内容:
我像这样使用它:
I'm using the following in Swift:
I'm using it like this:
如果您只知道图像视图的宽度,并且当图像的高度是动态的时,那么您需要根据给定的宽度缩放图像的高度,以删除图像上方和下方的空白。使用此处中的以下方法根据屏幕的标准宽度缩放图像的高度。
并从 cellForRowAtIndexPath: 方法中调用它,如下所示:
If you know only the width of the imageview and when the height of the image is dynamic then you need to scale the image's height according to the given width to remove the white spaces above and below your image. Use the following method from here to scale the height of the image according to the standard width of your screen.
And call it from your cellForRowAtIndexPath: method like this:
Swift 4 版本
Swift 4 version
上述方法从未给出所需的值。由于宽高比保持相同的宽高比,我们只需要简单的数学来计算值
检测宽高比
获取新值
The above mentioned methods never give the required values.As aspect fit maintains the same aspect ratio we just need simple maths to calculate the values
Detect the aspect ratio
Get the new values
为什么不使用操作系统函数AVMakeRectWithAspectRatioInsideRect?
Why not use the OS function AVMakeRectWithAspectRatioInsideRect?
我想使用
AVMakeRectWithAspectRatioInsideRect()
而不包含 AVFoundation 框架。因此,我实现了以下两个实用函数:
编辑:通过删除重复的分割进行优化。
编辑结束
这采用给定的大小(第一个参数)并保持其外观比率。然后,它在不违反纵横比的情况下尽可能填充给定的边界(第二个参数)。
用它来回答原来的问题:
注意图像如何确定长宽比,而图像视图如何确定要填充的大小。
非常欢迎反馈。
I wanted to use
AVMakeRectWithAspectRatioInsideRect()
without including the AVFoundation framework.So I've implemented the following two utility functions:
Edit: Optimized below by removing duplicate divisions.
End of edit
This takes a given size (first parameter) and maintains its aspect ratio. It then fills the given bounds (second parameter) as much as possible without violating the aspect ratio.
Using this to answer the original question:
Note how the image determines the aspect ratio, while the image view determines the size to be filled.
Feedback is very welcome.
请参阅@Paul-de-Lange的答案,而不是这个
我在易于访问的变量中找不到任何包含此内容的内容,所以这里是暴力方式:
Please see @Paul-de-Lange's answer instead of this one
I couldn't find anything in an easily accessible variable that had this, so here is the brute force way: