Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert points from image to UIImageView points, contentMode-aware

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.

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

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).

Thanks!

like image 733
nubbel Avatar asked Aug 27 '11 19:08

nubbel


1 Answers

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

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.

Hope you like it!

like image 63
nubbel Avatar answered Oct 18 '22 20:10

nubbel