如何从UIImageView中获取显示的图像帧?
我修改 UIImageView 的 contentMode 属性。所以我的图像的框架不等于 UIImageView 的框架。我的问题是如何从 UIImageView 检索图像的框架。
如下例所示,橙色矩形是 UIImageView 的边框。很明显,图像的框架与UIImageView不一样。
I modify the UIImageView's contentMode property. So my image's frame is not equal to the frame of UIImageView. My problem is how to retrive the image's frame from UIImageView.
As the following example, the orange rectangle is the UIImageView's border. It is obviously that the frame of image is not the same as UIImageView.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果是
UIViewContentModeScaleAspectFit
,则如下所示:If it's
UIViewContentModeScaleAspectFit
, it's something like this:事实上,它是从 iOS 4 开始内置的(对于
UIViewContentModeScaleAspectFit
):由于框架组件可能不是整数,请考虑通过
查看 http://nshipster.com/image-resizing/ 了解更多技巧。
In fact, it's built-in since iOS 4 (for
UIViewContentModeScaleAspectFit
):Since frame components may be not round numbers, consider wrapping the call by
See http://nshipster.com/image-resizing/ for more tricks.
使用 AVFoundation 的 api
Use this api from AVFoundation
现在很容易
Its very easy now
为了测试该功能是否正确,我使用了另一个视图来标识 UIImageView 中图像的边框。
完美!
To test if the function is right or not, I've used another view identifying the border of the image in UIImageView.
perfect!
最简单的方法是使用 AVMakeRectWithAspectRatioInsideRect。只需导入 AVFoundation,您就可以在一行代码中获取矩形。 Swift 4.一定要导入AVFoundation。
The easiest way is to use AVMakeRectWithAspectRatioInsideRect. Just import AVFoundation and you can get the rect in a single line of Code. Swift 4. Be sure to import AVFoundation.