How can I get the scale factor for UIImageView, which AspectFit mode?

how can i get the scale factor for UIImageView mode which is AspectFit?

That is, I have a UIImageView with AspectFit mode. The image (square) scales to my UIImageView frame, which is a good one.

If I want to get the scale size that was used (for example, 0.78 or something else), how can I get it directly?

I do not want to compare the width of the parent view with the width of the UIImageView, since the calculation should take into account the orientation, noting that I am scaling the square image in a rectangular view. Therefore, why was I a direct way to request a UIImageView to find out.

EDIT: I need it to work for both iPhone and iPad.

+8
ios iphone uiview uiimageview
source share
2 answers

I wrote a UIImageView category for this:

UIImageView + ContentScale.h

#import <Foundation/Foundation.h> @interface UIImageView (UIImageView_ContentScale) -(CGFloat)contentScaleFactor; @end 

UIImageView + ContentScale.m

 #import "UIImageView+ContentScale.h" @implementation UIImageView (UIImageView_ContentScale) -(CGFloat)contentScaleFactor { CGFloat widthScale = self.bounds.size.width / self.image.size.width; CGFloat heightScale = self.bounds.size.height / self.image.size.height; if (self.contentMode == UIViewContentModeScaleToFill) { return (widthScale==heightScale) ? widthScale : NAN; } if (self.contentMode == UIViewContentModeScaleAspectFit) { return MIN(widthScale, heightScale); } if (self.contentMode == UIViewContentModeScaleAspectFill) { return MAX(widthScale, heightScale); } return 1.0; } @end 
+16
source share

Ok, you could do something like

 CGFloat widthScale = imageView.image.size.width / imageView.frame.size.width; CGFloat heightScale = imageView.image.size.height / imageView.frame.size.height; 

Let me know if this works for you.

+1
source share

All Articles