gpt4 book ai didi

iphone - 如何获得模式为 AspectFit 的 UIImageView 的比例因子?

转载 作者:可可西里 更新时间:2023-11-01 03:06:05 32 4
gpt4 key购买 nike

如何获取模式为 AspectFit 的 UIImageView 的比例因子?

也就是说,我有一个模式为 AspectFit 的 UIImageView。图像(正方形)缩放到我的 UIImageView 框架,这很好。

如果我想获得使用的比例(例如 0.78 或其他),我该如何直接获得它?

我不想将父 View 宽度与 UIImageView 宽度进行比较,因为计算必须考虑方向,注意我正在将方形图像缩放为矩形 View 。因此,为什么我要通过直接查询 UIImageView 的方式来找出答案。

编辑:我也需要它来部署 iPhone 或 iPad。

最佳答案

我为此编写了一个 UIImageView 类别:

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

关于iphone - 如何获得模式为 AspectFit 的 UIImageView 的比例因子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6726423/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com