将点从图像转换为UIImageView点,contentMode感知 [英] Convert points from image to UIImageView points, contentMode-aware

查看:75
本文介绍了将点从图像转换为UIImageView点,contentMode感知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个显示在UIImageView(例如300 x 300像素)中的图片(例如1024 x 768像素)。
现在,我想转换一个点从图像,例如。一个人的鼻子的位置(x:500,y:600)到UIImageView上的相应点,并考虑其contentMode。

Let's say I have an image (e.g. 1024 x 768 px) which is displayed in a UIImageView (e.g. 300 x 300 px). Now, I'd like to convert a point from the image, e.g. the position of a person's nose (x: 500, y:600), to the corresponding point on the UIImageView with its contentMode taken into account.

如果contentMode固定在UIViewContentModeScaleToFill,则转换将很容易。但是如果它是UIViewContentModeScaleAspectFit,事情变得更复杂。

If the contentMode is fixed at UIViewContentModeScaleToFill, conversion will be easy. But if it's UIViewContentModeScaleAspectFit, things getting more complex.

有一个优雅的方式来实现吗?我真的不想计算每一个单一的contentMode(超过10,我想)。

Is there an elegant way to achieve that? I don't really want to calculate that for every single contentMode (more than 10, I think).

谢谢!

推荐答案

今天,我有一些空余时间把我的解决方案放在这个问题,并发布在GitHub上: UIImageView-GeometryConversion

Today, I've had some spare time to put together my solution to this problem and publish it on GitHub: UIImageView-GeometryConversion

这是UIImageView上的一个类别,提供了将图像中的点和矩形转换为视图坐标和方向的方法所有13种不同的内容模式。

It's a category on UIImageView that provides methods for converting points and rects from the image to view coordinates and respects all of the 13 different content modes.

希望你喜欢它!

这篇关于将点从图像转换为UIImageView点,contentMode感知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆