如何获得UIImageView的比例因子,其模式是AspectFit? [英] how can I get the scale factor of a UIImageView who's mode is AspectFit?
问题描述
如何获得UIImageView的比例因子,其模式是AspectFit?
how can I get the scale factor of a UIImageView who's mode is AspectFit?
也就是说,我有一个模式为AspectFit的UIImageView。图像(方形)缩放到我的UIImageView框架,这很好。
That is, I have a UIImageView with mode AspectFit. The image (square) is scaled to my UIImageView frame which is fine.
如果我想获得使用的比例量(例如0.78或其他),怎么能我直接得到这个?
If I want to get the amount of scale that was used (e.g. 0.78 or whatever) how can I get this directly?
我不想比较说父视图宽度与UIImageView宽度,因为计算必须考虑方向,注意我正在将方形图像缩放为矩形视图。因此,为什么我会直接查询UIImageView以找出答案。
I don't want to have to compare say a parent view width to the UIImageView width as the calculation would have to take into account orientation, noting I'm scaling a square image into a rectangular view. Hence why I was after a direct way to query the UIImageView to find out.
编辑:我也需要它才能用于iPhone或iPad部署。
I need it to work for iPhone or iPad deployment as well.
推荐答案
我为此编写了一个UIImageView类别:
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
这篇关于如何获得UIImageView的比例因子,其模式是AspectFit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!