Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I'm understanding this question correctly, you want to take a point, which is currently in relation to the layer's coordinate system, and convert it to the image view's coordinate system?</p> <p>In that case, there are a couple of ways to do this.</p> <p>Easiest is to use <code>convertPoint:fromView:</code> or <code>convertPoint:toView:</code></p> <blockquote> <p>CGPoint imageViewTouchPoint = [layerView convertPoint:touchPoint fromView:imageView];</p> <p>CGPoint imageViewTouchPoint = [imageView convertPoint:touchPoint toView:layerView];</p> </blockquote> <p>Either one should work.</p> <p><strong>EDIT</strong> - I realize now that this is only if the <code>UIImageView</code> has the same frame as the <code>UIImage</code>, which you said it might not, due to the <code>UIViewContentModeScaleAspectFit</code> property.</p> <p>In this case, unless I'm mistaken, the image frame is calculated inside the <code>UIImageView</code> <code>drawRect:</code> method and isn't a property that gets set. This means you'll have to calculate this on your own.</p> <p>Definitely get the <code>imageViewTouchPoint</code> from one of the methods above (just in case you want to use the same logic on a <code>UIImageView</code> which isn't the full screen size).</p> <p>You will then need to calculate the scaled image frame. There are a couple of ways to do this. Some people go brute force and manually calculate based on which side of the image is longer, then determining which side should be scaled. Then they calculate the origin by by centering the image and subtracting the image and image view's sides and dividing by two.</p> <p>I like to write as little code as possible if it's unnecessary, even if it means importing a framework. If you import <code>AVFoundation</code> you get a method <code>AVMakeRectWithAspectRatioInsideRect</code> which you can use to actually calculate the scaled rectangle in one line of code.</p> <blockquote> <p>CGRect imageRect = AVMakeRectWithAspectRatioInsideRect(image.size, imageView.frame);</p> </blockquote> <p>Whichever method you use, you will then simply translate your touched point with the scaled image origin:</p> <blockquote> <p>CGPoint imageTouchPoint = CGPointMake(imageViewTouchPoint.x - imageRect.origin.x, imageViewTouchPoint.y - imageRect.origin.y);</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload