I have a UIImageView which contains a scaled image. The image size is 3264 x 2448 and the UIImageView contentMode is set to AspectFit. The UIImageView's size is 320 x 427. I have the following view hierarchy:
WorkspaceView : UIView
-> UIImageView (Contains the image)
-> A UIView which contains a UITapGestureRecognizer
I am currently trying to get the coordinate from a touch based on the UIImageView (which has a large XY coordinate space - 2448). The UIView raises a touch based on its own view, not its superview. It has encapsulated the UITapGestureRecognizer so that it raises a delegate call.
How do I get the scaled XY touch point when given a coordinate inside of the UIView? I have tried the following code and it doesn't seem to work:
- (void)lineView:(LineView *)aLineView requestsDistanceForLine:(Line *)line {
// aLineView is the subview
// line contains the startpoint and endpoint of the aLineView - it's the model
// capturedImageView is the local instance of the scaled UIImageView
CGPoint startPoint =[self convertPoint:[line startPoint] toView:self.capturedImageView];
}
The startPoint ends up being too small. Any thoughts?
It turns out someone has already solved this problem. Here's the link to the github repository for it.
https://github.com/nubbel/UIImageView-GeometryConversion
Related
We're using CorePlot to draw a chart. In the yAxis we draw the temperature.
Given a temperature value, we want to find the y coordinate that corresponds to that temperature in the UIViewController's coordinate space.
Our view hierarchy is organized the following way:
_ UIViewController's main view (self.view)
\_ UIScrollView
\_ ChartView
Reading the docs, we've found -[CTPAxis viewPointForCoordinateDecimalNumber:] that should do exactly what we want.
Now, since that method would return the Y position in the ChartView's coordinate system, we use -[UIView convertPoint:toView:] to do the translation from the ChartView to the UIScrollView and then to the UIViewController's main view:
//self is the UIViewController
CGPoint *originalPoint = [self.temperatureAxis viewPointForCoordinateDecimalNumber:temperature];
CGPoint *scrollViewConvertedPoint = [self.chartView convertPoint:originalPoint toView:self.scrollView];
CGPoint *finalPoint = [self.scrollView convertPoint:scrollViewConvertedPoint toView:self.view];
Using this method, we're obtaining incorrect values as seen in the next picture.
Thanks in advance.
The originalPoint is in the coordinate space of the plot area. Convert it to the graph's coordinate space before doing the view conversions.
CGPoint graphPoint = [graph convertPoint:originalPoint
fromLayer:graph.plotAreaFrame.plotArea];
I need to resize an UImageView dynamically
for that I need to find the UImageview's absolute rect on screen, x,y is what i need actually.
because my ImageView is inside another view, if i query the ImageView frame, i get x,y of it to be 0,0
how do I find the absolute rect on screen ?
try using
CGRect targetFrame = [self.view convertRect:imageView.frame fromView:imageView];
I have a UIScrollView which contains a UIImageView
I need to zoom to a specific location relative to the UIImage and NOT the UIScrollView
So for example I have a UIImage where the size is 1000,1000 PX
I would like to zoom to a square such that CGRect = (400,500,100,100) inside that image
Unfortunately self.scrollView zoomToRect:animated isn't working properly because this rect is outside of its view and not in the same coordinate system
Also the aspect ratio of the UIImage can change inside of the UIImageview so it can be difficult to calculate its offset inside of the UIImageView (the black bars at the top and bottom of the image)
I know about
convertRect:fromView:
so thought about doing
-(void)zoomToRectInImage:(CGRect)rect
{
CGRect rect1 = [self.imageView.image convertRect:rect fromView:self.imageView.image]; // error because UIImage isn't a view
CGRect scrollViewRect = [self.scrollView convertRect:rect1 fromView:self.imageView];
[self.scrollView zoomToRect:scrollViewRect animated:YES]
}
I asked the question in a different format and this might help out someone
UIScrollView how to zoomToRect in UIImageView
I have a UIImageView that I am able to move on my screen. However, what I would like to know is the center point of the UIImageView after the user has stopped moving it. My UIImageView is located inside my main view.
I have tried using:
CGPoint centerPoint = _imageView.center;
but unfortunately, this value remains the same regardless of where I move the UIImageView on the screen. How can I determine the center point of the UIImageView that is dynamic due to the fact that the user will be moving the UIImageView on the screen?
You can do
CGRect frame = _imageView.frame;
And then do some math with the x coord, y coord, height, and width to get the center with respect to your main view.
I understand that in order to get the x or y coordinates of a UI Element you can use something like button.center.y or self.view.frame.size.height to get the height of your UIView). But, if I understand all this correctly, all of this is within the bounds of a UIView.
How do you obtain the same information in terms of the iPhone screen itself?
Maybe something like:
screen.view.frame.size.height
Thanks in advance.
EDIT: My UIElements are on a UIScrollView.
Convert the point from your view's coordinate system to the enclosing window's coordinate system, and then convert that point from the window to the screen.
Use -[UIView convertPoint:toView:] and -[UIWindow convertPoint:toWindow:]
UIWindow* myWindow = myView.window;
CGPoint pointInWindow = [myView convertPoint:pointInMyView toView:myWindow];
// note: toView:nil also works
CGPoint pointInScreen = [myWindow convertPoint:pointInWindow toWindow:nil];
By the way, an important point: view properties like center and frame are relative to the view's superview's coordinate system. So, to convert a view's center to the screen:
CGPoint pointInWindow = [myView.superview convertPoint:myView.center toView:nil];
CGPoint pointInScreen = [myView.window convertPoint:pointInWindow toWindow:nil];