How to get absolute rect on screen of UIImageView - ios

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];

Related

I want frame of a subview w.r.t to window and superview of the same subview is transformed ( scaled, rotated)

In the image we need to find the frame for inner square wrt to windows. Normat cgRect : to method is giving wrong answer.
A much better method than my previous answer, and will handle subviews regardless of ratio / centering.
You can get the "innerView" frame like this:
innerView.frame = outerView.convert(innerView.frame, to: view)

Adding imageView as subView

How can I add imageView as subView to imageView to it content end ?
And I need to add offset of imageView in its content size
My code not works
imageView.frame = CGRectMake(0, imageView.subviews.count * _mageView.frame.size.height + _imageView.frame.origin.y , _screenWidth, _image.size.height);
To answer the second question first, you might look at CGRectOffset(CGRect rect, x, y)
where rect is the original rectangle and x and y are the values by which the new rectangle is offset from the original rectangle. You can use this statement to define the value of imageView.frame.
For the first question, add the imageView as a subView of the mainView: [mainView addSubview:imageView].

Converting CGRect from UIImage to rect in UIScrollView in order to perform zoom

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

Need to find final location of moveable UIImageView in iOS

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.

How to get absolute image position in iOS?

Currently I have an image inside of a frame in my view.
I would like to get an absolute position of the image.
By using image.frame.origin.x I get a position relative to the frame where image is.
e.g if the frame is at the bottom I should get a large number x and y. But I receive 0 and 0 as image is right next to top and left border of the frame.
Try:
CGRect absolutePosition = [image convertRect:self.view.bounds toView:nil];
This will give you the position in the window. If you want it referenced to another view, change nil to that view.

Resources