Minkle Garg
Minkle Garg

Reputation: 751

Get dimension or CGRect of UIImage after AspectFit

I am just finding the way to get the image size whenever it aspect fit to imageview. but didn't get any proper way to get the image dimension. enter image description here I just want to calculate the difference between the imageview and the actual image in the imageview. Please advice.

Upvotes: 1

Views: 509

Answers (2)

ShineMan
ShineMan

Reputation: 354

Actually, when the image is showed, its size(instance of Image) doesn't change unless you call compressing image functions.

The only problem is how it's drawn on ImageView. When you use aspect fit to imageview, the image is drawn keeping its own rate between width and height based on the larger one of width and height of ImageView.

So, how to calculate the drawn image size is showed above by @Luan Tran.

Thanks

Hope it help

Upvotes: 1

Luan Tran
Luan Tran

Reputation: 1142

You can use this

 CGSize imageViewSize = imageView.bounds.size;
 CGSize imageSize = image.size;
 CGFloat minFactor = imageViewSize.width / imageSize.width;
 if (imageViewSize.height / imageSize.height < minFactor) 
 {
     minFactor = imageViewSize.height / imageSize.height;
 }

 CGSize resultSize = CGSizeMake(minFactor * imageSize.width, minFactor * imageSize.height);

Hope it help.

Upvotes: 1

Related Questions