0

I have a UIImageView which loads images with different aspect ratios. To display the full image properly I am using UIViewContentModeScaleAspectFit.

I need to be able to allow users to tap on image and tag friends. How do I calculate the X and Y percentages of the tap location relative to the actual image content - since UIImageView contains padding at top-bottom or sides to satisfy the UIViewContentModeScaleAspectFit constraint. I need to be able to isolate that out of the percentage calculation.

Also, the inverse needs to be done when the UIImageView needs to be rendered with the image and tags.

  • You could use : [AVMakeRectWithAspectRatioInsideRect][1] to get the rect of your image inside the UIImageView. Then it will just be a matter of offsetting your tap position with the returned rect position I think. (not tested) [1]: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVFoundation_Functions/Reference/reference.html – bperson Jul 27 '14 at 17:40
  • Thanks ddr2. I ended up using this solution instead http://stackoverflow.com/questions/6278876/how-to-know-the-image-size-after-applying-aspect-fit-for-the-image-in-an-uiimage – curvejumper Jul 29 '14 at 04:59
  • it's a bit more lengthy but it should work too :) – bperson Jul 29 '14 at 07:53

1 Answers1

0

The easy way is with the built in function AVMakeRectWithAspectRatioInsideRect.

[imageView setFrame:AVMakeRectWithAspectRatioInsideRect(image.size, imageView.frame)]

You can, of course, reinvent the wheel and do a bunch of calculations by hand.

LawfulEvil
  • 2,267
  • 24
  • 46