The image.size
attribute of UIImageView
gives the size of the original UIImage
. I would like to find out the size of the autoscaled image when it is put in the UIImageView
(typically smaller than the original).
For example, I have the image set to Aspect Fit
. Now I want to know its new height and width on the screen so I can draw accurately on the new scaled image.
Is there any way to do this without figuring it out myself based on the UIImageView size & UIImage original size (basically reverse engineering its scaling)?
If the frame is filled maintaining the image aspect ratio, the imageview frame will not be the same as the image size.
Following up on @MeetDoshi, here is an extension that should accommodate this.
Since you've got the image view set to Aspect Fit, you can simply take the height and width of the image view along with the height and width of the original image and calculate the height and width of the scaled image.
That said, the better way of doing this is to have a custom view instead of a UIImageView. Then, draw the image yourself in the custom view. That way you can control every aspect of it.
An adaptation of cncool answer, in swift as a UIImageView extension, perhaps it could be useful to someone:
This simple function will calculate size of image:-
For more details, You can refer AVMakeRectWithAspectRatioInsideRect - AVFoundation.
I wrote a useful extension that includes this in it's set of additions, might be worth checking out.
Included Functions
https://gist.github.com/nathan-fiscaletti/e1b85f68559f305db7342bfff9574563