how can I get the scale factor of a UIImageView who's mode is AspectFit?

前端 未结 2 1732
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-15 01:15

how can I get the scale factor of a UIImageView who\'s mode is AspectFit?

That is, I have a UIImageView with mode AspectFit. The image (square) is scaled to my UIIm

相关标签:
2条回答
  • 2020-12-15 01:29

    I've written a UIImageView category for that:

    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
    
    0 讨论(0)
  • 2020-12-15 01:38

    Well 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 that works for you.

    0 讨论(0)
提交回复
热议问题