Search code examples
iosios6uiimageviewuiimage

Easiest way to determine the size of an image when within an ImageView


I have a large image which is different in size all the time. The image is placed into a UIImageView. At present, for this example the image is kept in its original size but in future would be optimised to fit for performance.

Lets say an example 1 is : 1600x1200 An example 2 is: 900x2400

If a UIImageView takes up the whole view screen, what is the easiest way to determine the size of the image within the UIImageView.

Is there someway to determine this without doing the maths? If the image view has an Aspect to Fit style for content mode. Also if the view controller is shown in Landscape/Portrait the image would change in size shown. I am trying to determine the frame of the actual image within the UIImageView.

Of course with maths I could do something like this:

ratio = imageview.height / pushedPhoto.height
frameWidth = ratio * pushedPhoto.width
frameHeight = ratio * pushedPhoto.height
frameX = (imageview.height-frameHeight)/2;
frameY = (imageview.width-frameWidth)/2;

But I am wondering if there is an easier route to find this out.


Solution

  • Have to complete with maths for this. Please feel free to answer in the future, should there be an alternate.